There are two problems:
/ not part of the expression. They are delimiters denoting a regular expression literal. They need to be removed if you use RegExp , otherwise they will literally match the slash.
Secondly, the backslash is an escape character in string literals. To create a literal \ for an expression, you need to escape it in a string.
So the equivalent would be:
new RegExp("rt:([^@]+)@(\\d+)")
In particular, escaping makes an expression more difficult to write if you want to use RegExp . This is really only necessary if you want to dynamically create an expression, that is, if you want, for example, to include text stored in a variable. If you have a fixed expression, the literal /.../ easier to write and more concise.
Felix Kling Nov 01 '11 at 10:09 2011-11-01 10:09
source share