Checking conditions

Aba supports logical operators and comparisons that allow you to build complex conditions and choose the replacement based on them.

Logical operators

There are three logical operators:

An empty string, false, null, 0, and -0 are false values (can be converted to false). Everything else is a true value.

Search forReplace toExplanation
(\d{1,2})/(\d{1,2})(?:/(20\d{1,2}))? \(\3 || 2023) Match a date in the US format; replace it with the year if it's specified, otherwise replace with 2023
(\d{1,2})/(\d{1,2})(?:/(20\d{1,2}))? \(1 <= \1 && \1 <= 12 && 1 <= \2 && \2 <= 31 && 'valid' || 'invalid') Check that the month and the day are valid (between 1 and 31; we don't check the number of days in this month), replace the date with valid or invalid

Comparison operators

The usual comparison operators are supported:

Two strings are compared by their Unicode code point order, not linguistically, so you should not rely on the comparison operators for sorting.

When comparing a number to a string, the string is first converted to a numeric value. If it's not possible to convert, the string comparison is used. This allows you to compare the subexpressions (\1, \2, etc.) with a number literal if you want a numeric comparison, or with a string literal if you want a string comparison.

Search forReplace toExplanation
anything \('25' < '3') A string comparison ('2' < '3', so the result is true)
anything \('25' < 3) A numeric comparison (25 > 3, so the result is false)

The if ... else statement

if cond { value1 } else { defaultValue }
if cond { value1 } else if cond2 { value2 } else { defaultValue }

The if ... else statement consists of a condition and several branches. You can have multiple else if branches or none of them. The else statement is mandatory in the replacements; otherwise, Aba would not know what text to use by default. If you need to return an empty string, please write: else {''}

You can also nest if statements or concatenate results from two or more if statements. No parentheses are needed around the condition, but the braces are mandatory. In the conditions, you can use the logical operators (&&, ||, !) and comparisons.

Search forReplace toExplanation
20[0-9]{2} if \0 == '2024' { 'this year' } else { \0 } Replace with this year if the year is 2024 (this can be a part of a more complex replacement, otherwise you can just replace 2024 with this year)
20[0-9]{2} if \0 == '2024' { 'this year' } else if \0 == '2023' { 'previous year' } else { \0 } Also add the previous year for 2023

Note that unlike in C, Java, or JavaScript, the if ... else statement in Aba returns a value, so it's similar to the ternary operator in these languages.

This is a page from Aba Search and Replace help file.