问题描述
我正在尝试在Javascript中使用 John Gruber的URL regex ,但NetBeans不断告诉我有语法错误和非法错误:
I am trying to use John Gruber's URL regex in Javascript but NetBeans keeps telling me there is a syntax error and illegal errors:
var patt = "/(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])
|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]
{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|
(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|
(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:
'".,<>?«»""‘’]))/";
有人知道如何解决这个问题吗?
Anyone know how to solve this?
推荐答案
正如其他人所说,它是双引号.但是,或者,您也可以只将regexp写为javascript中的文字(但随后您需要转义第1行和第3行中的正斜杠).
As others have said, it's the double quote. But alternatively, you can just write the regexp as a literal in javascript (but then you need to escape the forward slashes in lines 1 and 3 instead).
var regexp = /\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»""‘’]))/i;
我也将不区分大小写的修饰符移到了最后.只是因为.(嗯,不只是因为"-参见下面的艾伦·摩尔评论)
I also moved the case-insensitive modifier to the end. Just because. (edit: Well, not just "because" - see Alan Moore's comment below)
注意:无论您使用文字还是字符串,必须在一行上.
Note: Whether you use a literal or a string, it has to be on 1 line.
这篇关于URL正则表达式在javascript中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!