我试图在PHP中翻译JavaScript脚本。到目前为止,会好的,但我碰到一些代码上我无言以对跌跌撞撞:Javascript lastIndex正则表达式属性到PHP正则表达式
while (match = someRegex.exec(text)) {
m = match[0];
if (m === "-") {
var lastIndex = someRegex.lastIndex,
nextToken = someRegex.exec(parts.content);
if (nextToken) {
...
}
someRegex.lastIndex = lastIndex;
}
}
的someRegex
变量是这样的:
/[^\\-]+|-|\\(?:[0-3][0-7]{0,2}|[4-7][0-7]?|x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4}|c[A-Za-z]|[\S\s]?)/g
高管应相当于在PHP中preg_match_all:
preg_match_all($someRegex, $text, $match);
$match = $match[0]; // I get the same results so it works
foreach($match as $m){
if($m === '-'){
// here I don't know how to handle lastIndex and the 2nd exec :(
}
}
哪里'parts'来自于JavaScript的? – 2013-03-26 14:58:13
这是一个由另一个正则表达式构建的一些属性的对象。在PHP中,我将它作为一个关联数组(它看起来一样) – Alex 2013-03-26 15:01:31