我試圖在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