开发者

how to a regex for similar string

i have this string

[url=test.php]test[/url]

or

[url=http://test.php]text[/url]

I want to a regex get this

[url=http://test.php]text[/开发者_Go百科url]

example:

before:

str = "[url=test.php]text[/url]"
str.gsub(/\[url=(.*?)\](.*?)\[\/url\]/,'[url=http://\1]\2[/url]')
=> "[url=http://test.php]text[/url]"

question:

str = "[url=http://test.php]text[/url]"
str.gsub(/\[url=(.*?)\](.*?)\[\/url\]/,'[url=http://\1]\2[/url]')
=> "[url=http://http://test.php]text[/url]"

so.. two "http://"

Any ideas anyone?

thanks


Thanks for clarifying your question.

You need a regex that only matches [url] BBCode tags if the linked URL is not starting with http.

\[url=\s*(?!http)([^\]]+)\](.*?)\[/url\]

should work:

str.gsub(/\[url=\s*(?!http)([^\]]+)\](.*?)\[\/url\]/,'[url=http://\1]\2[/url]')

Explanation:

\[url=\s*   # match [url= literally, plus optional space
(?!http)    # assert that it's not possible to match http here
([^\]]+)    # match 1 or more characters except ], capture in \1
\]          # match ]
(.*?)       # match link text, capture in \2
\[/url\]    # match [/url]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜