最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - differences of special characters in regex: .net vs javascript - Stack Overflow

programmeradmin3浏览0评论

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 pare my implementation to the page at .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?

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 pare my implementation to the page at http://msdn.microsoft./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?

Share Improve this question edited May 8, 2011 at 1:07 John Saunders 162k26 gold badges252 silver badges402 bronze badges asked May 7, 2011 at 21:00 PacerierPacerier 89.9k111 gold badges385 silver badges645 bronze badges 1
  • Please see "Should 'Hi', 'thanks' and taglines and salutations be removed from posts?" – John Saunders Commented May 8, 2011 at 1:10
Add a ment  | 

2 Answers 2

Reset to default 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.

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.

发布评论

评论列表(0)

  1. 暂无评论