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);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…