I've got this implementation in javascript:
EscapeForRegex = function(input) {
var specials = ["[", "\\", "^", "$", ".", "|", "?", "*", "+", "(", ")", "{", "}"]
for (var k in specials) {
var special = specials[k];
input = input.replace(new window.RegExp("\\" + special, "g"), "\\" + special);
}
return input;
};
however when i compare my implementation to the page at http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.escape.aspx, i find 2 differences.
I've included ] but the page doesn't do so. is it true that we do not have to include the ] ? (apparently I'm not doubting that page, but since my implementation is in javascript and not c#/vb..)
I've missed out #. is the # symbol special in javascript regex?
1) I've included ] but the page doesn't do so. is it true that we do not have to include the ] ? (apparently I'm not doubting that page, but since my implementation is in javascript and not c#/vb..)
]
only has to be escaped inside a character set. That list is also missing -
which needs to be escaped inside character sets sometimes. E.g., to create a character set containing the characters space, dash, and the letter A, you would need to escape the -
thus: /[ \-A]/
or move the dash to the side: /[- A]/
.
Of the characters that you listed above, only ]
, -
, ^
, and \\
ever need to be escaped in character sets. ^
only needs to be escaped inside a character set if it is in the character set and at the beginning.
If you want to include the regular expression text inside the literal form, /.../
instead of new RegExp("...")
you also need to escape line terminator characters: codepoint U+000A, U+000D, U+2028, U+2029, and the /
character when outside a character set.
2) I've missed out #. is the # symbol special in javascript regex?
No, #
is not special in JavaScript.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With