0
我使用下面的正則表達式來檢查URL是否有效URL檢查在瀏覽器
var re = /^(http[s]?:\/\/(www\.)?|ftp:\/\/(www\.)?|www\.){1}([0-9A-Za-z-\[email protected]:%_+~#=]+)+((\.[a-zA-Z]{2,3})+)(/(.)*)?(\?(.)*)?/;
var is_valid = re.test(input_url);
它適用於小的投入,但是開始運行無盡的較大投入運行無盡的正則表達式。考慮以下64個字符輸入
re.test("http://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
運行此用了最新的谷歌Chrome瀏覽器中運行時不會分鐘內完成。
正則表達式有問題嗎?
[災難性回溯(http://www.regular-expressions.info/catastrophic.html )在[([0-9A-Za-z - \。@:%_ +〜#=] +)+'中嵌套量詞)移除其中一個'+'...更改爲'([0 -9A-Za-z - \。@:%_ +〜#=] +)' – Mariano
這裏還有其他的瓶頸,問題是:各種類型的嵌套量詞。 –
@Mariano太棒了,謝謝! – muffel