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

Simple jQueryjavascript method to escape special characters in string for regexp - Stack Overflow

programmeradmin4浏览0评论

I'm performing a regular expression using the match() method on a string that es from the user and could contain anything, including $^'s etc.. so I need to escape those characters before this happens.

Is there a mon 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?)

I'm performing a regular expression using the match() method on a string that es from the user and could contain anything, including $^'s etc.. so I need to escape those characters before this happens.

Is there a mon 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?)

Share Improve this question edited Sep 25, 2012 at 20:41 Brian Webster 30.9k51 gold badges157 silver badges226 bronze badges asked Mar 15, 2011 at 0:07 John HuntJohn Hunt 4,0729 gold badges46 silver badges61 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 13

Found a function here:

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

评论列表(0)

  1. 暂无评论