Sometimes it's convenient to refer to the matched regular expression groups in the replacement string of the String::replace() method. Let's say we want to replace nodefunction(somenode, blah, blah) with somenode.evaluate(blah, blah) everywhere in a string s.
A simple solution which comes to mind is:
s.replace(/nodefunction\(([a-zA-Z0-9_]+)\s*,/g, "\\1.evaluate(")
where "\1" is the first matched group ("somenode").
But it doesn't work. Group references are not documented in the String::replace() method's reference. I tried some other common group symbols used in other regexp engines (&, $), and found that the feature is actually supported. This replacement syntax works:
s.replace(/nodefunction\(([^,]+)\s*,/g, "$1.evaluate("))
It would be nice to update the documentation.
Other String::replace() issues I'd like to see addressed:
- document supported character classes (\s seems to work, but it's nowhere in the docs)
- document regexp assertions (ECMAscript style regexp assertions like ^, $, \b, \B, (?=), (?!) seem to work, but are not documented; see an image below)
- support multi-line regular expression matching (similar to /m modifier in ECMAscript's regexp syntax)
- use the same regexp dialect in the Find and Replace dialog as well as in Flexscript (see a screenshot below).
Supported, but undocumented regular expression assertions in Flexscript:
The syntax of the Find and Replace regular expressions is different from the syntax of Flexscript String::replace() regular expressions. Note different escaping rules and a different group reference (\1 instead of $1):
// Edit: reposted as an idea.