This matches URLs so that I can linkify them:
这与URL匹配,以便我可以链接它们:
(\b[a-zA-Z0-9-._+]@[a-zA-Z0-9.-]+[a-zA-Z0-9-]\b|\b([a-zA-Z]+:\/\/|[a-zA-Z]+:\/\/www\.|www\.)[a-zA-Z0-9.-]+[a-zA-Z0-9-]([:]*[0-9]*)(\/[-~+=%!:#@_a-zA-Z0-9.]*[-~+=:%!#_a-zA-Z0-9])*\/?(\/?\?[-+=&;:%@_.a-zA-Z0-9]*[-+=:&;%@_a-zA-Z0-9])?(#[-=_a-zA-Z0-9]+)?)
But I want to avoid matching URLs that are in HTML e.g. <img src="http://foo.com"/>
.
但我想避免匹配HTML中的网址,例如。
My thought was to update the regex so that it did not match if the URL was surrounded by single or double quotes. I think this would nicely cover all cases where the URL was in an HTML attribute and therefore shouldn't be mucked with.
我的想法是更新正则表达式,以便在URL被单引号或双引号包围时不匹配。我认为这很好地涵盖了URL属于HTML属性的所有情况,因此不应该被混淆。
How do I achieve this in JS regex?
我如何在JS正则表达式中实现这一目标?
1 个解决方案
#1
For this you need to use negative lookahead and lookbehind assertions, but for js negative lookahead doesn't work. So maybe negative lookbehind assertion would be enough.
为此你需要使用负向前瞻和后观断言,但对于js负向前瞻不起作用。所以也许负面的背后断言就足够了。
I would suggest to use already existing pattern (from here) for example:
我建议使用已经存在的模式(从这里),例如:
/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/
And modify it a bit adding: \w(?!\")
so the URL finishes with letter followed by not double quotes.
并修改它添加:\ w(?!\“)所以URL完成字母后跟不是双引号。
You can play with it here: https://regex101.com/r/hY8sZ8/1
你可以在这里玩它:https://regex101.com/r/hY8sZ8/1
#1
For this you need to use negative lookahead and lookbehind assertions, but for js negative lookahead doesn't work. So maybe negative lookbehind assertion would be enough.
为此你需要使用负向前瞻和后观断言,但对于js负向前瞻不起作用。所以也许负面的背后断言就足够了。
I would suggest to use already existing pattern (from here) for example:
我建议使用已经存在的模式(从这里),例如:
/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/
And modify it a bit adding: \w(?!\")
so the URL finishes with letter followed by not double quotes.
并修改它添加:\ w(?!\“)所以URL完成字母后跟不是双引号。
You can play with it here: https://regex101.com/r/hY8sZ8/1
你可以在这里玩它:https://regex101.com/r/hY8sZ8/1