使用$来匹配输入的结尾,在其他地方会得到一个零长度的匹配,但在WebKit中没有匹配的证据:
function showBug() {
Result = "the end.".replace( /(end\.)([\s]|$)?/img, makeChange );
return;
}
function makeChange() {
for ( var i = 0; i < arguments.length; i += 1 ) {
document.write( "arg" + i + " -->" + arguments[ i ] + "<--" + "<BR>" );
}
}
提供
arg0 -->end.<--
arg1 -->end.<--
arg2 -->undefined<--
arg3 -->4<--
arg4 -->the end.<--
对于
AppleWebKit/534.55.3 (KHTML, like Gecko) Version/5.1.5 Safari/534.55.3
和 AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19
,都适用。 Opera (Presto/2.10.229 Version/11.62
)、FF (Gecko/20100101 Firefox/10.0.2
) 和 IE (MSIE 8.0; Trident/4.0
) 都有相同的结果。arg0 -->end.<--
arg1 -->end.<--
arg2 --><--
arg3 -->4<--
arg4 -->the end.<--
这意味着我可以在$2中检测到匹配项(实际上是关于将url末尾的句点解释为不属于url的问题)。目前,我正在为WebKit添加一个尾随空格,然后再将其删除,但我想知道是否有更好的解决方案,并确认我是否应该将其视为错误。
|
。因此,您可以将其编写为/(end\.)([\s])?$/
以匹配行末之前的内容。其次,您是要匹配 0 或 1 个 's' 和 '',还是要匹配空格\s
(没有方括号)?http://jsfiddle.net/2hZdT/ - Evan DavisResult = "the end.abc".replace( /(end\.)([\s]|$)?/img, makeChange );
将告诉makeChange()
不要考虑解释点。 - anweald