Arj*_*Raj 1 javascript regex jquery
我想检查给定字符串中多个字符串的可用性(不使用循环).
喜欢
my_string = "How to find occurence of multiple sting in a given string using javascript RegExp";
// search operated on this string
// having a-z (lower case) , '_' , 0-9 , and space
// following are the strings wanted to search .( having a-z , 0-9 , and '_')
search_str[0]="How";
search_str[1]="javascript";
search_str[2]="sting";
search_str[3]="multiple";
Run Code Online (Sandbox Code Playgroud)
是否有可用于此的正则表达式?
更新:我错过了什么
在答案中,我发现这一个正在解决上述问题
if (/^(?=.*\bHow\b)(?=.*\bjavascript\b)(?=.*\bsting\b)(?=.*\bmultiple\b)/.test(subject)) {
// Successful match
}
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,它无法正常工作.
m_str="_3_5_1_13_10_11_";
search_str[0]='3';
search_str[1]='1';
tst=new RegExp("^(?=.*\\b_"+search_str[0]+"_\\b)(?=.*\\b_"+search_str[1]+"_\\b)");
if(tst.test(m_str)) alert('fooooo'); else alert('wrong');
Run Code Online (Sandbox Code Playgroud)
if (/^(?=.*\bHow\b)(?=.*\bjavascript\b)(?=.*\bsting\b)(?=.*\bmultiple\b)/.test(subject)) {
// Successful match
}
Run Code Online (Sandbox Code Playgroud)
这假设您的字符串不包含换行符.如果是,则需要将所有.s 更改为[\s\S].
我使用了单词边界锚来确保Howard或resting不小心提供匹配.如果您确实想要允许,请删除\bs.
说明:
(?=...)是一个先行断言:它在字符串中向前看以检查所包含的正则表达式是否可以 在当前位置匹配而不实际消耗匹配的字符.因此,一连串的前瞻工作就像一系列正则表达式(锚定到字符串的开头^)与逻辑&&运算符组合在一起.
| 归档时间: |
|
| 查看次数: |
318 次 |
| 最近记录: |