Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

differences of special characters in regex: .net vs javascript

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.

  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..)

  2. I've missed out #. is the # symbol special in javascript regex?

like image 873
Pacerier Avatar asked May 07 '11 21:05

Pacerier


1 Answers

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.

like image 50
Mike Samuel Avatar answered Nov 10 '22 00:11

Mike Samuel