Reputation: 113385
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?
Upvotes: 1
Views: 1907
Reputation: 81
var reg = /\[URL\=([^\]]+)\](.*)\[\/URL\]$/;
var str = '[URL=http://domain.com]My Awesome Link Text[/URL]';
var result = str.match(reg);
the result val:
["[URL=http://domain.com]My Awesome Link Text[/URL]", "http://domain.com", "My Awesome Link Text"]
you can use the result:
var html = "<a href=\"" + result[1] + "\">" + result[2] + "</a>";
other sample test:
maybe the link text include []
or other tag [red][/red]
,like this
"[URL=http://domain.com]My Awesome [red]Link Text[/red][/URL]"
so the first answer is not the best choice,because it not allow the link text include[
or]
Upvotes: 1
Reputation: 3903
Tested in Javascript.
[URL=([^\]]+)\]([^\[]+)\[\/URL\]
Not splitting : decomposing into groups.
t = '[URL=http://domain.com?param=value]My Awesome Link Text[/URL]'
r = /\[URL=([^\]]+)\]([^\[]+)\[\/URL\]/;
t.match(r)
--> ["[URL=http://domain.com?param=value]My Awesome Link Text[/URL]", "http://domain.com?param=value", "My Awesome Link Text"]
Upvotes: 1
Reputation: 174706
Pattern:
^.*?\[URL=([^\]]*)\]([^\[]*).*$
Replacement:
<a href="$1">$2</a>
In JavaScript:
> "[URL=http://domain.com?asd=a]My Awesome Link Text[/URL]".replace(/^.*?\[URL=([^\]]*)\]([^\[]*).*$/, '<a href="$1">$2</a>')
'<a href="http://domain.com?asd=a">My Awesome Link Text</a>'
Upvotes: 1
Reputation: 3646
Don't split, just parse it!
a.match(/\[URL=(.+)\](.+)\[\/URL\]/i)
And we get:
["[URL=http://domain.com]My Awesome Link Text[/URL]", "http://domain.com", "My Awesome Link Text"]
Upvotes: 1