JS中正则表达式全局匹配模式 /g用法详解

网络编程 发布日期:2024/9/30 浏览次数:1

正在浏览:JS中正则表达式全局匹配模式 /g用法详解

本文章来详细介绍js中正则表达式的全局匹配模式 /g用法,代码如下:

var str = "123#abc"; 
var re = /abc/ig; 
console.log(re.test(str)); //输出ture 
console.log(re.test(str)); //输出false 
console.log(re.test(str)); //输出ture 
console.log(re.test(str)); //输出false 

在创建正则表达式对象时如果使用了“g”标识符或者设置它了的"htmlcode">

var str = "123#abc"; 
var re = /abc/ig; 
console.log(re.test(str)); //输出ture 
console.log(re.lastIndex); //输出7 
console.log(re.test(str)); //输出false 
console.log(re.lastIndex); //输出0 
console.log(re.test(str)); //输出ture 
console.log(re.lastIndex); //输出7 
console.log(re.test(str)); //输出false 
console.log(re.lastIndex); //输出0 

以上所述是小编给大家介绍的JS中正则表达式全局匹配模式 /g用法详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!