求助:javascript正则表达式多次对同一字符串测试时,得到不同的结果 var tablesNotInputCharRegex = /^'$/ig;alert(tablesNotInputCharRegex.test("'"));alert(tablesNotInputCharRegex.test("'"));alert(tablesNotInputCharRegex.test("'"));出现的结果为truefalsetrue求解??? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 改成 var tablesNotInputCharRegex = /^'$/; 就正常了这和全局匹配模式有关 把g去掉就行 在g的模式下 每次匹配成功后他的开始索引index都会向后移动 view plaincopy to clipboardprint?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 var str = "123#abc";var re = /abc/ig;console.log(re.test(str)); //输出tureconsole.log(re.test(str)); //输出falseconsole.log(re.test(str)); //输出tureconsole.log(re.test(str)); //输出false 在创建正则表达式对象时如果使用了“g”标识符或者设置它了的global属性值为ture时,那么新创建的正则表达式对象将使用模式对要将要匹配的字符串进行全局匹配。在全局匹配模式下可以对指定要查找的字符串执行多次匹配。每次匹配使用当前正则对象的lastIndex属性的值作为在目标字符串中开始查找的起始位置。lastIndex属性的初始值为0,找到匹配的项后lastIndex的值被重置为匹配内容的下一个字符在字符串中的位置索引,用来标识下次执行匹配时开始查找的位置。如果找不到匹配的项lastIndex的值会被设置为0。当没有设置正则对象的全局匹配标志时lastIndex属性的值始终为0,每次执行匹配仅查找字符串中第一个匹配的项。可以通下面的代码来查看在执行匹配相应的lastIndex 属性的值。view plaincopy to clipboardprint?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 var str = "123#abc";var re = /abc/ig;console.log(re.test(str)); //输出tureconsole.log(re.lastIndex); //输出7console.log(re.test(str)); //输出falseconsole.log(re.lastIndex); //输出0console.log(re.test(str)); //输出tureconsole.log(re.lastIndex); //输出7console.log(re.test(str)); //输出falseconsole.log(re.lastIndex); //输出0关于RegExp.prototype.exec(str)方法和String.prototype.math(rgExp)方法 正则对象的test方法返回值为true或flase,在仅需要检测目标字符串与指定模式是否匹配,但不需要获取匹配内容时这个方法非常有用。当需要获取匹配结果时就需要用RegExp类型的exec(str)方法或String类型的match(rgExp)方法。 RegExp.prototype.exec(str)方法返回NULL或返会一个数组,在数组的第0个元素存放的是在字符串str中查找到的匹配内容,1到n个元素返回的是在模式中使用括号"()"指定的子匹配项的内容。 在没有使用全局标志时String.prototype.math(rgExp)方法和RegExp.prototype.exec(str)的行为类似。当设置了全局匹配标志时String.prototype.math(rgExp)方法返回的数组项元素0到n中包含了所有匹配到的项不包含子匹配项。这时可以使用RegExp.$1..$9获取9个子匹配。 jquery动态删除问题 Java WEB开发模式 JS定时器问题求助!! 页面比较长,页面提交后,停留在当前操作的位置 JSP代码小问题!!!求教 js 如何关闭窗口 表格在页面上的布局问题 百度播放器允许一个弹出窗,如何实现 在IFRAME中显示FTP窗口 可以让div的大小固定,如果其中内容过多,则自动出现滚动条吗? 懂JS的速度进来看一下,送分了,在线等! 什么是clientLeft啊
这和全局匹配模式有关
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 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”标识符或者设置它了的global属性值为ture时,那么新创建的正则表达式对象将使用模式对要将要匹配的字符串进行全局匹配。在全局匹配模式下可以对指定要查找的字符串执行多次匹配。每次匹配使用当前正则对象的lastIndex属性的值作为在目标字符串中开始查找的起始位置。lastIndex属性的初始值为0,找到匹配的项后lastIndex的值被重置为匹配内容的下一个字符在字符串中的位置索引,用来标识下次执行匹配时开始查找的位置。如果找不到匹配的项lastIndex的值会被设置为0。当没有设置正则对象的全局匹配标志时lastIndex属性的值始终为0,每次执行匹配仅查找字符串中第一个匹配的项。可以通下面的代码来查看在执行匹配相应的lastIndex 属性的值。view plaincopy to clipboardprint?
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 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关于RegExp.prototype.exec(str)方法和String.prototype.math(rgExp)方法 正则对象的test方法返回值为true或flase,在仅需要检测目标字符串与指定模式是否匹配,但不需要获取匹配内容时这个方法非常有用。当需要获取匹配结果时就需要用RegExp类型的exec(str)方法或String类型的match(rgExp)方法。 RegExp.prototype.exec(str)方法返回NULL或返会一个数组,在数组的第0个元素存放的是在字符串str中查找到的匹配内容,1到n个元素返回的是在模式中使用括号"()"指定的子匹配项的内容。 在没有使用全局标志时String.prototype.math(rgExp)方法和RegExp.prototype.exec(str)的行为类似。当设置了全局匹配标志时String.prototype.math(rgExp)方法返回的数组项元素0到n中包含了所有匹配到的项不包含子匹配项。这时可以使用RegExp.$1..$9获取9个子匹配。