我需要将bbcode标记添加到没有任何标记的URL中。我目前正在使用正则表达式来查找它们,但是某些情况下存在问题。 这是我正在测试的情况:
www.example.com //OK
http://example.com //OK
http://www.example.com //OK
http://www.example.com/test/ //OK
http://www.example.com/test/image.jpg //OK
www.example.com\nSome random text //not working, should find www.example.com only
Some random text\nhttp://www.example.com //OK
http://example.com\nSome random text //not working, should find http://example.com only
http://www.example.com\nSome random text //not working, should find http://www.example.com only
Some random text\nhttp://www.example.com\nSome random text //not working, should find http://www.example.com only
http://www.example.com\nhttp://www.example.com/test/image.jpg\n //not working should find both http://www.example.com and http://www.example.com/test/image.jpg
[IMG]http://www.example.com/test/image.jpg[/IMG] //OK
[URL]http://www.example.com[/URL] //OK
[url=http://www.example.com]Domain[/url] //OK
[url=http://www.example.com]http://www.example.com[/url] //OK
我正在使用Notepad ++进行测试,正则表达式为:
(?:(?<!(\[\/url\]|\[\/url=)))(((https?|ftps?|ircs?):\/\/\S+)|(www\.\S+))(?:(?<![[:punct:]])|(?<=\/))(\s|\.?$)
所以我想我最大的问题是正则表达式不止于\ n字符。