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
436 views
in Technique[技术] by (71.8m points)

javascript - 正则表达式用于脚本,不区分大小写不起作用[重复](regex for appscript, case insensitivy not working [duplicate])

This question already has an answer here:

(这个问题已经在这里有了答案:)

Below I am trying to use regex to get the occurrence of certain strings.

(下面,我试图使用正则表达式来获取某些字符串的出现。)

  var value = "ab-2123 AB-332";
  // "i" is for case insensitive
  var regExp = new RegExp("(ab)[-][0-9]*", "gi"); 
  var searchedString = regExp.exec(value);
  console.log(searchedString); 

This only detects ab-2124 but not AB-33 .

(这仅检测到ab-2124 ,而不检测到AB-33 。)

Could some one please help me to figure out the mistake?

(有人可以帮我找出错误吗?)

  ask by hadi k translate from so

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

1 Reply

0 votes
by (71.8m points)

Firstly, a few important typos corrected

(首先,纠正了一些重要的错别字)

var searchedString regExp.exec(value) should be var searchedString = regExp.exec(value)

(var searchedString regExp.exec(value)应该是var searchedString = regExp.exec(value))

console.log(searched string); should be console.log(searchedString);

(应该是console.log(searchedString);)


regExp.exec() will only return your first match.

(regExp.exec()将仅返回您的第一个匹配项。)

If you want multiple, you could use String.match(regExp)

(如果要多个,可以使用String.match(regExp))

 let value = "ab-2123 AB-332"; let regExp = new RegExp("(ab)[-][0-9]*", "gi"); // "i" is for case insensitive let searchedString = value.match(regExp); console.log(searchedString); 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...