(……续例13)正则表达式
例14:全局组替换
ide
- void function reg_14()
- {
- var s = "Live for nothing nothing";
- var r = /([a-z]+) \1/g;
- if (r.exec(s) != null)
- {
- alert(RegExp.$1);
- alert(s.replace(r, RegExp.$1)); //JScript不支持命名组
- }
- }
这个例子很是简单,用正则表达式pia找到两个重复的单词,而后去掉其中一个。
这里使用了String对象的replace()方法。spa
(未完待续……)对象