javascript正则表达式的的标识问题
<script language= "javascript ">
var Re = /[0-9]/;
document.write(Re.test( "0 "));
document.write(Re.test( "0 "));
document.write(Re.test( "0 "));
</script>
结果为true true true;
而
<script language= "javascript ">
var Re = /[0-9]/g;
document.write(Re.test( "0 "));
document.write(Re.test( "0 "));
document.write(Re.test( "0 "));
</script>
的结果却是true false true.
相差一个[g],为什么会这样?
谢谢!
------解决方案--------------------会这样?
------解决方案--------------------g为全局匹配
当一个字符在全局匹配一次后下次就不用它了
<script language= "javascript ">
var Re = /[0-9]/g;
document.write(Re.test( "01 "));
document.write(Re.test( "01 "));
document.write(Re.test( "01 "));
</script>
不加g
则每次都会从头开始匹配
所以每次都为true