注意JavaScript中RegExp对象的test方法

2016-02-19 22:17 10 1 收藏

只要你有一台电脑或者手机,都能关注图老师为大家精心推荐的注意JavaScript中RegExp对象的test方法,手机电脑控们准备好了吗?一起看过来吧!

【 tulaoshi.com - Web开发 】

  javascript 中的 RegExp 对象用于正则表达式相关的操作,这个对象提供了一个方法 test 来判定某个字符串是否满足某个 pattern. 返回值是 true/false.

  今天我碰到了一个问题:

script type="text/javascript"
!--
var re = /^d+(?:.d)?$/ig;   

alert(re.test('112.3'));
alert(re.test('33'));
//--
/script

  这里两个测试的字符串应该都满足正则表达式中的模式,返回 true. 可是测试结果却依次是:true, false.

  我估计问题的原因可能是因为 RegExp 对象是有状态的,并且在 test 方法的执行时会在某个步骤中利用到状态信息,这样就造成了错误。
  (注:RegExp 全局对象有一些静态属性和方法,比如 RegExp.$1... RegExp$9, 等)

  解决这个问题的办法也很简单,就是每次重新初始化一次正则表达式对象:

script type="text/javascript"
!--
alert(/^d+(?:.d)?$/ig.test('112.3'));
alert(/^d+(?:.d)?$/ig.test('33'));
//--
/script
在我看来,JavaScript 中正则表达式的这个行为设计的很奇怪,应该说是和正常使用习惯有那么一点点的不同。虽然使用了很久的 JavaScript, 却一直没有注意到这个奇怪的现象。其他语言比如 Python, C# 等都不是这样的。

  了解这个问题详细原因的朋友,请不吝指教。

posted on 2007-01-08 15:23 木野狐 阅读(330) 评论(1)  编辑 收藏 引用 网摘 所属分类: ASP,JS,CSS  

Feedback
# re: 注意 JavaScript 中 RegExp 对象的 test 方法 2007-01-08 23:33 Derek
lastIndex Property See Also
RegExp Object Properties | Regular Expression Syntax

Applies To: RegExp Object
Requirements
Version 3
Returns the character position where the next match begins in a searched string.

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

RegExp.lastIndex
The object associated with this property is always the global RegExp object.

Remarks
The lastIndex property is zero-based, that is, the index of the first character is zero. Its initial value is –1. Its value is modified whenever a successful match is made.

The lastIndex property is modified by the exec and test methods of the RegExp object, and the match, replace, and split methods of the String object.

The following rules apply to values of lastIndex:

If there is no match, lastIndex is set to -1.
If lastIndex is greater than the length of the string, test and exec fail and lastIndex is set to -1.
If lastIndex is equal to the length of the string, the regular expression matches if the pattern matches the empty string. Otherwise, the match fails and lastIndex is reset to -1.
Otherwise, lastIndex is set to the next position following the most recent match.

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

http://www.cnblogs.com/RChen/archive/2007/01/08/regexp_test.html

来源:https://www.tulaoshi.com/n/20160219/1628112.html

延伸阅读
标签: Web开发
JS中定义类的方式有很多种: 1、工厂方式   function Car(){    var ocar = new Object;    ocar.color = "blue";    ocar.doors = 4;    ocar.showColor = function(){     doc...
标签: Web开发
1、关于javascript的apply和call函数 prototype.js中用了大量的apply和call函数,不注意会造成理解偏差。 官方解释:应用某一对象的一个方法,用另一个对象替换当前对象。 apply与call的区别是第二个参数不同。apply是  数组或者arguments 对象。而call是逗号隔开的任何类型。 apply,call方法最让人混淆的地方也是apply,c...
标签: Web开发
JavaScript中的History历史对象包含了用户已浏览的 URL 的信息,是指历史对象指浏览器的浏览历史。鉴于安全性的需要,该对象收到很多限制,现在只剩下下列属性和方法。History历史对象有length这个属性,列出历史的项数。JavaScript 所能管到的历史被限制在用浏览器的“前进”“后退”键可以去到的范围。本属性返回的是“前进”...
标签: Web开发
这是基本JavaScript的屏幕对象 screen 屏幕对象 反映了当前用户的屏幕设置。 width 返回屏幕的宽度(像素数)。 height 返回屏幕的高度。 availWidth 返回屏幕的可用宽度(除去了一些不自动隐藏的类似任务栏的东西所占用的宽度)。 availHeight 返回屏幕的可用高度。 colorDepth 返回当前颜色...
标签: Web开发
a id="link1" name="link1" href="http://bbs.51js.com/"51js/a ============= 同一页面内的引用方法: 1、使用id: link1.href 2、使用name: document.all.link1.href 3、使用sourseIndex: document.all(4).href //注意,前面还有HTML、HEAD、TITLE和BODY,所以是4 4、使用链接集合: document.ancho...

经验教程

264

收藏

77
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部