Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

javascript - 在Javascript正则表达式中使用的转义字符串[重复](Escape string for use in Javascript regex [duplicate])

Possible Duplicate:

(可能重复:)

I am trying to build a javascript regex based on user input:

(我正在尝试根据用户输入构建javascript正则表达式:)

function FindString(input) {
    var reg = new RegExp('' + input + '');
    // [snip] perform search
}

But the regex will not work correctly when the user input contains a ?

(但是,当用户输入包含?时,正则表达式将无法正常工作?)

or * because they are interpreted as regex specials.

(或*因为它们被解释为正则表达式特殊项。)

In fact, if the user puts an unbalanced ( or [ in their string, the regex isn't even valid.

(实际上,如果用户在字符串中输入不平衡([ ,则正则表达式甚至无效。)

What is the javascript function to correctly escape all special characters for use in regex?

(什么是可以正确转义要在正则表达式中使用的所有特殊字符的javascript函数?)

  ask by too much php translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to reply this article.

OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...