开发者

Simple jQuery / javascript method to escape special characters in string for regexp

开发者 https://www.devze.com 2023-02-17 07:09 出处:网络
I\'m performing a regular expression using the match() method on a string that comes from the user and could contain anything, including $^\'s etc.. so I need to escape those chara开发者_运维知识库cte

I'm performing a regular expression using the match() method on a string that comes from the user and could contain anything, including $^'s etc.. so I need to escape those chara开发者_运维知识库cters before this happens.

Is there a common function in jQuery to do this, a well known javascript function or am I going to have to do it manually (with the chance I might miss something?)


Found a function here:

RegExp.escape = function(text) {
    return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}
0

精彩评论

暂无评论...
验证码 换一张
取 消