简体   繁体   English

正则表达式忽略区分大小写的字符串Java脚本

[英]Regex to ignore case sensitive String Java script

I am working on a requirement where I need to allow couple of values in a text box before submitting the form. 我正在处理一项要求,在提交表单之前,我需要在文本框中允许几个值。 The below regex is working fine but it is allowing strings other than "testing" and "dev". 下面的正则表达式可以正常工作,但它允许使用“ testing”和“ dev”以外的字符串。 Important thing is it should ignore case sensitive .It should allow "dev","DEV","dEv" etc... 重要的是它应该忽略大小写。它应该允许“ dev”,“ DEV”,“ dEv”等。

pattern="(?i)^(?:testing|dev|)$" 

The /i flag should get you where you want to go: /i标志应该将您带到您想去的地方:

pattern = text.match(/^(?:testing|dev|)$/i);

text.match(pattern);

Regexr: http://regexr.com/3e6nd Regexr: http: //regexr.com/3e6nd

This code will return an array with the first match for any of the words in the array. 此代码将返回一个数组,该数组与该数组中的任何单词的第一个匹配项。 Note that the word Development or thetesting will be not a match because it uses the word boundary assertion \\b . 请注意,单词Developmentthetesting将不匹配,因为它使用单词boundary assertion \\b

Code: 码:

 var userInput = 'Development thetesting devs Dev testING test testIng', result = userInput.match(/\\b(testing|dev)\\b/i); console.log(result && result[0]); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM