7

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?

1

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

2

FYI, your function can be reduced to:

function EscapeForRegex(input){
    return input.replace(/[(-.]|[$?[\]\\^|{}]/g, '\\$&');
}

which doesn't include #, and does include ] and -, as pointed out by Mike Samuel.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.