regexp测试函数行为 [英] regexp test function behavior

查看:51
本文介绍了regexp测试函数行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不理解RegExp.test函数的一些行为。


示例html代码:

--------- -------

< html>< head>< / head>< body>< script type =" text / javascript">

var r = / ^ https?:\ / \ // g;

document.write([

r.test(''http:/ / a''),

r.test(''http:// b''),

r.test(''http:// c'' ),

r.test(''http:// d'')

]);

< / script>< / body>< / html>

---------------------


页面显示true,false,true,false。 (在Opera,Firefox和IE中)

这很奇怪,因为我预计它会显示true,true,true,

true。必须有一些我不知道的功能

RegExp.test。

I coudn''t understand some behavior of RegExp.test function.

Example html code:
----------------
<html><head></head><body><script type="text/javascript">
var r = /^https?:\/\//g;
document.write( [
r.test(''http://a''),
r.test(''http://b''),
r.test(''http://c''),
r.test(''http://d'')
]);
</script></body></html>
---------------------

The page displays true, false, true, false. (in Opera, Firefox and IE)
This is strange because I expected it would display true, true, true,
true. There must be something I didn''t know about the function
RegExp.test.

推荐答案

HopfZ写道:


[snip]
HopfZ wrote:

[snip]

var r = / ^ https?:\ / \ // g ;

document.write([

r.test(''http:// a''),

r.test(' 'http:// b''),

r.test(''http:// c''),

r.test(''http:/ / d'')

]);
var r = /^https?:\/\//g;
document.write( [
r.test(''http://a''),
r.test(''http://b''),
r.test(''http://c''),
r.test(''http://d'')
]);



[snip]

[snip]


页面显示true,false,true,false。 (在Opera,Firefox和IE中)

这很奇怪,因为我预计它会显示true,true,true,

true。必须有一些我不了解的功能

RegExp.test。
The page displays true, false, true, false. (in Opera, Firefox and IE)
This is strange because I expected it would display true, true, true,
true. There must be something I didn''t know about the function
RegExp.test.



全局标志是造成混淆的原因。它甚至没有让它包括在内:你使用带有

输入启动断言的表达式[1](^)和只能匹配一次。


RegExp.prototype.test方法等同于表达式,


re.exec(str) != null


,当使用RegExp.prototype.exec方法

时,全局标志很重要。匹配后,正则表达式

对象的lastIndex属性被修改为指向前一个

匹配子字符串的末尾。在下次调用exec方法时,这个

位置用于开始下一次搜索。


在第一次调用结束时,lastIndex属性将在比赛结束时指向超出

(对于角色,''a'')。在尝试匹配第二次调用中的输入启动断言(^)时,断言将会失败(在字符串开始后尝试匹配)。这些

尝试将一直持续到字符串结束为止,此时
指向lastIndex属性重置为零并返回null。通过

重置lastIndex属性,第三个调用可以正常进行,如第一个调用

。第四个电话将是第二个电话的重复。


Mike

[1]使用多行标志,它也可以作为一个行开始

断言,但这不适用于此。

The global flag is the cause of your confusion. It doesn''t even make
sense for it to be included: you''re using an expression with an
input-start assertion[1] (^) and that could only ever match once.

The RegExp.prototype.test method is equivalent to the expression,

re.exec(str) != null

and the global flag is significant when the RegExp.prototype.exec method
is used. After a match, the lastIndex property of the regular expression
object is modified to point just beyond the end of the previously
matched sub-string. On the next invocation of the exec method, this
position is used to begin the next search.

At the end of the first call, the lastIndex property will point beyond
the end of the match (to the character, ''a''). Whilst attempting to match
the input-start assertion (^) in the second call, the assertion will
fail (the match is attempted after the start of the string). These
attempts will continue until the end of the string is reached, at which
point the lastIndex property is reset to zero and null is returned. With
the lastIndex property reset, the third call can proceed normally like
the first. The fourth call will be a repeat of the second.

Mike
[1] With the multi-line flag, it also acts as a line-start
assertion, but that doesn''t apply here.


迈克尔·温特写于29 okt 2006 comp.lang.javascript
Michael Winter wrote on 29 okt 2006 in comp.lang.javascript:

全局标记是导致混淆的原因。它甚至没有让它包括在内:你使用带有

输入启动断言的表达式[1](^)和只能匹配一次。
The global flag is the cause of your confusion. It doesn''t even make
sense for it to be included: you''re using an expression with an
input-start assertion[1] (^) and that could only ever match once.



更重要的是,在test()中设置全局标志永远不会有任何意义。

Even more so, setting the global flag in a test() never makes any sense.


在第一次调用结束时,lastIndex属性将指向超出

匹配结束
At the end of the first call, the lastIndex property will point beyond
the end of the match



一个很好的解释。


即便如此它也是一个错误!!!!


全局标志应该导致错误,

或在test()中被忽略。


======================== ===========


测试:


< script type =''text / javascript''> ;


// IE7测试


var r = / x / g;

document.write(r 。试验( '' X '')+ '' <峰; br> ''); // true

document.write(r.test(''x'')+''< br>''); // false

document.write(r.test(''x'')+''< br>''); // true

document.write(r.test(''x'')+''< br>''); // false

document.write(''< br>'');

r = / x / g;

document.write (r.test( '' X '')+ '' <峰; br> ''); // true

r = / x / g;

document.write(r.test(''x'')+''< br>''); // true

r = / x / g;

document.write(r.test(''x'')+''< br>''); // true

r = / x / g;

document.write(r.test(''x'')+''< br>''); // true


document.write(''< br>'');

r = / x /;

文件撰写(r.test( '' X '')+ '' <峰; br> ''); // true

document.write(r.test(''x'')+''< br>''); // true

document.write(r.test(''x'')+''< br>''); // true

document.write(r.test(''x'')+''< br>''); // true

< / script>


-

Evertjan。

荷兰。

(请在我的电子邮件地址中将x'变为点数)

A good explanation.

Even so it is a bug!!!!

The global flag should either lead to an error,
or be disregarded in test().

===================================

Testing:

<script type=''text/javascript''>

// IE7 tested

var r = /x/g;
document.write(r.test(''x'')+''<br>''); // true
document.write(r.test(''x'')+''<br>''); // false
document.write(r.test(''x'')+''<br>''); // true
document.write(r.test(''x'')+''<br>''); // false
document.write(''<br>'');
r = /x/g;
document.write(r.test(''x'')+''<br>''); // true
r = /x/g;
document.write(r.test(''x'')+''<br>''); // true
r = /x/g;
document.write(r.test(''x'')+''<br>''); // true
r = /x/g;
document.write(r.test(''x'')+''<br>''); // true

document.write(''<br>'');
r = /x/;
document.write(r.test(''x'')+''<br>''); // true
document.write(r.test(''x'')+''<br>''); // true
document.write(r.test(''x'')+''<br>''); // true
document.write(r.test(''x'')+''<br>''); // true
</script>

--
Evertjan.
The Netherlands.
(Please change the x''es to dots in my emailaddress)


Evertjan。写道:


[snip]
Evertjan. wrote:

[snip]

更重要的是,在test()中设置全局标志永远不会产生任何

感。
Even more so, setting the global flag in a test() never makes any
sense.



从不?我不知道。很少见,肯定。


例如,计算字符串中模式出现次数的一种方法是使用String.prototype.match方法[1]:


var result = string.match(regExp),

count = result? result.length:0;


其中regExp是一个设置了全局标志的正则表达式对象。

但是,也可以用RegExp做同样的事情.prototype.test方法:


函数countMatches(字符串,模式){

var count = 0,

index = pattern .lastIndex = 0;


while(pattern.test(string)){

++ count;

if(pattern .lastIndex == index){++ pattern.lastIndex;}

}

返回计数;

}

var count = countMatches(string,regExp);


在Fx和Op中更容易使用,效率稍高 -

测试很简单:区分大小写的单字符搜索。尽管在MSIE中它的速度较慢,但​​性能仍然优于Fx或Op。


[snip]

Never? I don''t know about that. Rare, certainly.

For example, one way to count the number of occurrences of a pattern
within a string is to use the String.prototype.match method[1]:

var result = string.match(regExp),
count = result ? result.length : 0;

where regExp is a regular expression object with the global flag set.
However, one could also do the same with the RegExp.prototype.test method:

function countMatches(string, pattern) {
var count = 0,
index = pattern.lastIndex = 0;

while (pattern.test(string)) {
++count;
if (pattern.lastIndex == index) {++pattern.lastIndex;}
}
return count;
}

var count = countMatches(string, regExp);

Marginally easier to use, and slightly more efficient in Fx and Op - the
test was simple: a case-sensitive, single character search. Even though
it''s slower in MSIE, performance is still better than in either Fx or Op.

[snip]


即便如此,这也是一个错误!!!!


全局标志应该导致错误,

或者在test()中被忽略。
Even so it is a bug!!!!

The global flag should either lead to an error,
or be disregarded in test().



完全没有。责任归咎于使用全球旗帜的开发商

,它不属于,或者在上次调用

a之后未能重置lastIndex属性。


[snip]


Mike

[1]浏览器从RegExp.prototype.match方法返回null
当为正则表达式设置全局标志

对象且未找到匹配项时。在我看来,

15.5.4.10,ECMA-262 3rd Ed。会要求一个空数组。

这不是什么大不了的事,但它会使上面的例子更简单。

Not at all. The blame would fall on the developer who used a global flag
where it didn''t belong, or failed to reset the lastIndex property after
a previous invocation.

[snip]

Mike
[1] Browsers return null from the RegExp.prototype.match method
when both the global flag is set for the regular expression
object and no matches are found. It seems to me that
15.5.4.10, ECMA-262 3rd Ed. would call for an empty array.
Not that big a deal, but it would make the example above a
bit simpler.


这篇关于regexp测试函数行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆