0

我想将链接与正则表达式匹配,直到第一个空格或<发生。我试过这个正则表达式

\b(((http|ftp)(.)?\:\/\/)?(www\.)?example\.com([^\s|<]+)?)\b

但是这个正则表达式的问题是它也是匹配example.com.au的。所以我想匹配什么

example.com                      // match
example.com/somelink/link        // match

example.com.au                   // do not match
example.com.au/somelink/link     // do not match

匹配到第一个空格或<发生

4

2 回答 2

1

这是一个匹配http://example.com/whatever但不匹配http://example.com.au/whatever的解决方案。

/\b(((http|ftp)(.)?:\/\/)?(www\.)?example\.com(?!\.[\w\d])(\/[^\s<]*)?)\b/

对此文本进行了测试:

Match http://example.com/ but not http://example.com.au
This is a sentence about http://example.com/.
http://example.com<
http://example.com/asdf.asdf.asdf/ asdf
http://example.computer

它使用否定前瞻来明确排除 example.com 后跟\.[\w\d].

于 2013-04-03T01:40:52.050 回答
0

告诉它你不想匹配 .com 之后的任何点

\b(((http|ftp)(.)?\:\/\/)?(www\.)?example\.com([^\s|<|\.]+)?)\b

或者变得更聪明,告诉它如果 .com 后面有任何内容,您希望在 .com 后面加上正斜杠

\b(((http|ftp)(.)?\:\/\/)?(www\.)?example\.com(\/[^\s|<]+)?)\b
于 2013-04-03T01:23:34.983 回答