内容全写注释里了- -
复制代码 代码如下:
<script type="text/javascript">
/^$/.test('\n'); //false,为什么呢,说明如下
var p = /^/mg;
var s = '1\n\n\n2\n\n3';
p.test(s); //跳过开始位置JS里好象没那个词- -
p.test(s);
alert(RegExp.rightContext.replace(/\x0A/g, '\\a'));
//由此得出^匹配的是\n后面的位置或开始的位置
var p = /$/mg;
var s = '1\n\n\n2\n\n3';
p.test(s);
alert(RegExp.rightContext.replace(/\x0A/g, '\\a'));
//由此得出$匹配的是\n前面的位置或结尾的位置
var p = /^\s*$/mg;
var s = '1\n\n\n2\n\n3';
alert(s.replace(p, function (l, index) {
alert(index);
return '';
}).replace(/\x0A/g, '\\a'));
var p = /^\s*$/mg;
var s = '1\n\n\n2\n\n3';
p.test(s); //true
alert(RegExp.lastIndex); //3
p.test(s); //true
alert(RegExp.lastIndex); //强制驱动后的结果即4
p.test(s); //true
alert(RegExp.lastIndex); //7
p.test(s); //false
alert(RegExp.lastIndex); //匹配失败没设置global的RegExp对象
/^X$/g.test('a'); //false
alert(RegExp.lastIndex); //注:匹配失败是不会设置global的RegExp对象的
/^X$/.test('X'); //true
alert(RegExp.lastIndex); //匹配成功当然会设置^^
</script>
