0
我試圖用正則表達式解析堆棧跟蹤,但它似乎有一些主要的性能問題,或者更可怕的錯誤。當我使用代碼運行時,瀏覽器似乎永遠掛起,當我在控制檯中運行它時,它可能需要10或15秒才能返回。我正在測試鉻。Javascript正則表達式導致瀏覽器掛起
下面是一個例子:
"Object.printStackTrace.implementation.createException (file:///F:/billysFile/code/javascript/nodejs/deadunit/browserPackage/deadunit.browser.gen.umd.js:942:19)"
.match(/^(((new)?[a-zA-Z_$][\w$]*(.[a-zA-Z_$][\w$]*)*)\(\)@((?:http|https|file):\/\/[^\s)]+|javascript:.*)(:(\d*):(\d*))|((new)?[a-zA-Z_$][\w$]*(.[a-zA-Z_$][\w$]*)*) \(((?:http|https|file):\/\/[^\s)]+|javascript:.*)(:(\d*):(\d*))\)|((new)?[a-zA-Z_$][\w$]*(.[a-zA-Z_$][\w$]*)*) (\(native\)))$/)
和這裏的合成前原正則表達式:
"Object.printStackTrace.implementation.createException (file:///F:/billysFile/code/javascript/nodejs/deadunit/browserPackage/deadunit.browser.gen.umd.js:942:19)".match(CHROME_STACK_LINE)
var IDENTIFIER_PATTERN_ = '[a-zA-Z_$][\\w$]*';
var URL_PATTERN_ = '((?:http|https|file)://[^\\s)]+|javascript:.*)';
var CHROME_FILE_AND_LINE = URL_PATTERN_+'(:(\\d*):(\\d*))'
var CHROME_COMPOUND_IDENTIFIER = "((new)?"+IDENTIFIER_PATTERN_+'(\\.'+IDENTIFIER_PATTERN_+')*)'
var CHROME_ANONYMOUS_FUNCTION = CHROME_COMPOUND_IDENTIFIER+'\\(\\)'+'@'+CHROME_FILE_AND_LINE
var CHROME_NORMAL_FUNCTION = CHROME_COMPOUND_IDENTIFIER+' \\('+CHROME_FILE_AND_LINE+'\\)'
var CHROME_NATIVE_FUNCTION = CHROME_COMPOUND_IDENTIFIER+' (\\(native\\))'
var CHROME_FUNCTION_CALL = '('+CHROME_ANONYMOUS_FUNCTION+"|"+CHROME_NORMAL_FUNCTION+"|"+CHROME_NATIVE_FUNCTION+')'
var CHROME_STACK_LINE = new RegExp('^'+CHROME_FUNCTION_CALL+'$')
我做得可怕的錯誤嗎?它看起來不像我catastrophic backtracing ..
恭喜!你確實有某種病態的正則表達式。 '(。[a-zA-Z _ $] [\ w $] *)*'可能是罪魁禍首。 – Ryan
也許我應該只使用PEG .. –
也許你應該分裂''「'。取決於你想要提取的內容。 – Ryan