javaScript高级教程(八)-----正则表达式温故知新

1.RegExp对象:五个属性二个方法

五个属性:global, ignoreCase,multiline,lastIndex,source

二个方法:

exec()--模式匹配

test()--检测一个字符串是否含有某个模式

2.例子

var pattern = /Javaw*/g;
var text = 'JavaScript is more fun than Java or JavaBeans!';
var result;
while((result = pattern.exec(text)) != null){
  console.log('Matched! '+result[0] +' at position '+result.index +' next search begins at position '+pattern.lastIndex);
  
}
//结果
Matched! JavaScript at position 0 next search begins at position 10
Matched! Java at position 28 next search begins at position 32
Matched! JavaBeans at position 36 next search begins at position 45
原文地址:https://www.cnblogs.com/yuyutianxia/p/3278899.html