1

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?

4 Answers 4

1

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>'

DEMO

Sign up to request clarification or add additional context in comments.

Comments

1

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"]

Comments

1

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"]

Comments

1
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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.