JavaScript 使用正则表达式操作字符串

var testString = "Ada Lovelace and Charles Babbage designed the first computer and the software that would have run on it.";

var expression = /and/gi;

var andCount = testString.match(expression).length;

 

/ 是这个正则表达式的头部

the 是我们想要匹配的模式

/ 是这个正则表达式的尾部

g 代表着 global(全局),意味着返回所有的匹配而不仅仅是第一个。

i 代表着忽略大小写,意思是当我们寻找匹配的字符串的时候忽略掉字母的大小写。

posted @ 2019-06-10 21:36  风一样的man  阅读(393)  评论(0编辑  收藏  举报