Manipulating with strings
It's possible to do string operations with the match \0
, with the subexpressions (\1
, \2
, etc.), or with the filename.
String literals
Strings should be enclosed into double or single quotes. The following escape sequences are supported:
- a carriage return
\r
and a newline\n
, - a tab character
\t
, - a single quote
\'
or a double quote\"
, - a literal backlash
\\
, - an ASCII character like
\xB9
(B9 is the hexadecimal character code), - a Unicode character like
\u03B4
(U+03B4 is the Unicode code point), - a Unicode character outside of Basic Multilingual Plane like
\U01F642
for U+1F642,
There must be exactly 2 hexadecimal digits after \x
, 4 digits after \u
, and 6 digits after \U
. For example, if your code point is U+1F642, you have to add a zero before it. Of course, you also can insert a Unicode character into a string; you don't have to use the escape sequences.
Search for | Replace to | Explanation |
anything |
\("'test'") |
'test' in single quotes |
anything |
\('\'test\'') |
Same; when in single quotes, we have to escape |
anything |
\('"test"') |
"test" in double quotes (enclosed into single quotes to avoid the escaping) |
anything |
\('1\xA0234') |
one, then U+00A0 no-break space, then 234 (using the non-break space as a thousands separator) |
anything |
\('\u2014') |
U+2014 em dash |
anything |
\('—') |
the same character inserted literally |
Concatenation
There is no explicit concatenation operator. To concatenate several strings, write them next to one another:
Search for | Replace to | Explanation |
anything |
\('be' 'come') |
The result is become |
\<([0-9]{1,2})/([0-9]{1,2})/((?:20)?[0-9]{2})\> |
\(\2 '.' \1 '.' \3) |
Replace a date in the US format (month/day/year) with the same date in the European format (day.month.year) |
\<([0-9]{1,2})/([0-9]{1,2})/((?:20)?[0-9]{2})\> |
\2.\1.\3 |
Same using simple replacements |
\<function (\w{2,}) |
function \(\1[0].toUpper() \1[1:]) |
Make the first letter of a function name uppercase |
You can mix the concatenation with math operators, but if you have an unary minus or an unary not in the expression, please add parentheses:
Search for | Replace to | Explanation |
anything |
\(5 ' ' -20+5) |
The result is -5-15 ; the space is gone because it's first concatenated with −20, then converted into a number |
anything |
\(5 ' ' (-20+5)) |
The correct way: use parentheses |
anything |
\('result: ' -20+5) |
An error; the string is first concatenated with −20, then Aba cannot add 5 to it |
anything |
\('result: ' (-20+5)) |
The correct solution |
Indexing and slicing
You can extract the n-th character of a string. The indexes are counted from zero; negative indexes are counted from the end of the string:
Search for | Replace to | Explanation |
\w+ |
\(\0[0]) |
The first character of each word |
\w{2,} |
\(\0[1]) |
The second character |
\w+ |
\(\0[-1]) |
The last character |
\w{2,} |
\(\0[-2]) |
The next to last character |
The index should range from minus string length to the string length minus one, otherwise Aba shows an error message.
Aba counts Unicode scalar values, not bytes, so an index of the next character is always an index of the previous character plus one (except for combining characters).
You can also extract a part of a string using positive or negative indices:
Search for | Replace to | Explanation |
\w{2,} |
\(\0[0:2]) |
The first two characters of each word that is at least two characters long |
\w{2,} |
\(\0[:2]) |
Same |
\w{2,} |
\(\0[2:]) |
All characters excepts for the first two |
^^ |
\(Aba.fileName()[-3:]) |
The last three characters of the filename (usually, it's the file extension, but it won't work for .json or .gitignore) |
When slicing, both indices can span from minus string length to the string length. Unlike JavaScript or Python, Aba shows an error message if the slice index is out of bounds or if the first index is larger than the second one.
The toLower/toUpper functions
str.toLower() str.toUpper()
These functions convert the string to lowercase/uppercase including Unicode characters. If the string contains non-alphabetic characters such as digits or spaces, they are not changed. The function returns a lowercase/uppercase equivalent of the string; the original string is not changed.
Search for | Replace to | Explanation |
\w+ |
\(\0.toLower()) |
Convert all words to lowercase |
\w{2,} |
\(\0[0].toUpper() \0[1:].toLower()) |
Start all words longer than two characters with a capital letter and make the remaining characters small letters |
The trim functions
str.trimLeft() str.trimLeft(charset) str.trimRight() str.trimRight(charset) str.trim() str.trim(charset)
These functions strip whitespace (or other characters) from the beginning or the end of the string. The trimLeft
function removes whitespace from the beginning of the string; the trimRight
function removes it from the end of the string; and the trim
function removes it from both sides of the string.
The charset
is the characters that you want to remove; if it's not specified, then the functions remove spaces, tab characters, newlines, and carriage returns. The functions return the string with whitespace removed; the original string is not changed.
Search for | Replace to | Explanation |
\N+ |
\(\0.trim()) |
Remove whitespace from the beginning and the end of each line |
\N+ |
\(\0.trimRight()) |
Remove whitespace from the end of each line |
[ \t]+$ |
|
Same without complex replacements |
\N+ |
\(\0.trimLeft(' ')) |
Remove only spaces (not tabs) from the beginning of each line |
^ + |
|
Same without complex replacements |
The indexOf function
str.indexOf(pattern)
This function searches the string and returns the first found index of the pattern in the string, or −1 if it's not found. The pattern
is a string; the function returns an index (starting from zero) that can be used for slicing or checking if the string contains a given substring.
If the pattern
or the str
string is an empty string, the function always returns −1.
Search for | Replace to | Explanation |
<a href="(https?://[^"]+)" |
<a href="\( if \1.indexOf('example.com') >= 0 {\1.toLower()} else {\1} )" |
Convert all links containing example.com to lowercase, but leave the other links unchanged |
The replace function
str.replace(pattern, replacement) str.replace(pattern, replacement, count)
This function replaces all or some occurrences of the pattern with the replacement string. The pattern
and the replacement
parameters are strings; the optional count
parameter is a number. If it's not specified, all occurrences of the pattern
are replaced.
The count
must be a positive integer, otherwise an error message is displayed. The function returns a copy of the string with the pattern occurrences
replaced with the replacement; the original string is not changed.
Search for | Replace to | Explanation |
^^ |
<img src="\{Aba.fileName().replace('.html', '.png')}"> |
Insert an image with the same name as the current HTML file |
The len function
str.len()
The function returns the string length. If the string is empty, it returns zero. Note that Aba counts Unicode scalar values, not bytes or grapheme clusters.
Search for | Replace to | Explanation |
$$ |
\( if Aba.filePath().len() > 0 {'banner'} else {''} ) |
If the file is located in a nested directory, add a banner |
This is a page from Aba Search and Replace help file.
- Welcome to Aba
- Getting started
- How-to guides
- Selecting the files to search in
- Inserting some text at the beginning of each file
- Replacing multiple lines of text
- Searching in Unicode files
- Replacing in binary files
- Performing operations with the found files
- Undoing a replacement
- Saving search parameters for further use
- Removing private data
- Adding or removing Aba from Explorer context menu
- Integrating Aba with Total Commander
- Integrating Aba with Free Commander
- Integrating Aba with Directory Opus
- Regular Expressions
- Replacement syntax
- Simple replacements
- Complex replacements
- Doing math
- Manipulating with strings
- Checking conditions
- Reading files and inserting file names
- User interface
- Command line
- Troubleshooting
- Glossary
- Version history
- Credits