Idea

Serge A avatar image
2 Likes"
Serge A suggested anthony.johnson edited

Document regexp groups and other regexp features in String::replace()

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.

flexscriptregexp
· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

anthony.johnson avatar image anthony.johnson ♦♦ commented ·

FlexScript's string class uses c++'s standard regex functionality, using the ECMAScript grammar, and the design was guided by JavaScript's regular expression implementation. So when in doubt, just use either the c++ reference or the JavaScript reference, as they are almost exactly the same. The main difference from JavaScript I can remember is that in FlexScript we have not (yet) implemented the /m, /y, or /u flags (we do support /i and /g). If we make any changes to the documentation, it will be merely to point users to the JavaScript or c++ documentation, along with any points where we deviate from that implementation. Their documentation is much more thorough than what we would want to provide.

The Find Replace dialog uses scintilla's grammar (yes we should consolidate those).

2 Likes 2 ·

No Comments

Your Opinion Counts

Share your great idea, or help out by voting for other people's ideas.