admin管理员组文章数量:1410737
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.
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?
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.
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?
- Please see "Should 'Hi', 'thanks' and taglines and salutations be removed from posts?" – John Saunders Commented May 8, 2011 at 1:10
2 Answers
Reset to default 51) 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.
本文标签: cdifferences of special characters in regex net vs javascriptStack Overflow
版权声明:本文标题:c# - differences of special characters in regex: .net vs javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744890325a2630739.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论