I have input strings like below:
[URL=http://...]Lorem ipsum[/URL]
They should be converted in HTML tags:
<a href="http://...">Lorem ipsum</a>
Example:
[URL=http://domain.com]My Awesome Link Text[/URL]
This should be converted in:
<a href="http://domain.com">My Awesome Link Text</a>
I tried to split the string using a regular expression:
> a = "[URL=http://domain.com]My Awesome Link Text[/URL]"
'[URL=http://domain.com]My Awesome Link Text[/URL]'
> s = a.split(/\[|\]|=/)
[ '',
'URL',
'http://domain.com',
'My Awesome Link Text',
'/URL',
'' ]
> o = "<a href=\"" + s[2] + "\">" + s[3] + "</a>"
'<a href="http://domain.com">My Awesome Link Text</a>'
This works fine for links that don't contain = in their url. But things get complicated when we have querystring parameters:
> a = "[URL=http://domain.com?param=value]My Awesome Link Text[/URL]"
'[URL=http://domain.com?param=value]My Awesome Link Text[/URL]'
> s = a.split(/\[|\]|=/)
[ '',
'URL',
'http://domain.com?param',
'value',
'My Awesome Link Text',
'/URL',
'' ]
> o = "<a href=\"" + s[2] + "\">" + s[3] + "</a>"
'<a href="http://domain.com?param">value</a>'
How to split such strings, but not splitting the = inside of the url?