Are there special strings for that, such as \Q and \E in Java? Thanks.
-
They should work the same. I did not down-vote but, I suggest you edit your question to show an example string + expected output. Also show your AS3 code of how you handled this regular expression. It'll become easier to see your problem that way.. – VC.One Feb 15 '16 at 17:22
-
@VC.One Are you sure? I think `\Q` and `\E` are special to Java, not part of RegEx. – Aaron Beall Feb 16 '16 at 16:14
-
@Aaron, my bad. I remember reading that AS3 uses same RegEX engine as JavaScript, somehow I made the [illogical] jump to assuming same for Java too.. – VC.One Feb 19 '16 at 18:14
2 Answers
As far as I know there is no equivalent to \Q
and \E
in AS3 RegExp. What you can do is the same thing you would in Javascript: escape special characters for use within a RegExp
pattern:
function escapeForRegExp(s:String):String {
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
}
// Example:
var s:String = "Some (text) with + special [regex] chars?";
var r:RegExp = new RegExp("^" + escapeForRegExp(s), "g");
// Same as if you wrote the pattern with escaped characters:
/^Some \(text\) with \+ special \[regex\] chars\?/g

- 1
- 1

- 49,769
- 26
- 85
- 103
Short answer is no, you have to escape each characters one at a time with \
as described
http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7e91.html
If it is really an issue, you could imagine writing your own escaping function and using:
new RegExp(escape("your text (using reserved characters ^^) [...]"))
instead of the constant syntax
/your text \(using reserved characters \^\^\) \[\.\.\.\]/
If you don't want to escape the whole regex, just concatenate the escaped & non-escaped parts
escape() function could be a RegExp one-liner, just prepending '\' to any reserved character

- 888
- 6
- 12