I have a string
var str="Hello my name is {john/www.john} and wele to my {site/www.site}."
i have extracted curly brackets and made an anchor tag out of them like
<a href="www.john">john</a>
What i am trying to do is replace curly brackets and content in them with these nodes. Is it possible using regExp? I have studied regExp on MDN but still cant figure out the way.
I have a string
var str="Hello my name is {john/www.john.} and wele to my {site/www.site.}."
i have extracted curly brackets and made an anchor tag out of them like
<a href="www.john.">john</a>
What i am trying to do is replace curly brackets and content in them with these nodes. Is it possible using regExp? I have studied regExp on MDN but still cant figure out the way.
Share Improve this question asked Jun 28, 2015 at 15:14 DarlynDarlyn 4,93812 gold badges53 silver badges98 bronze badges1 Answer
Reset to default 16Sure it is:
var str = "Hello my name is {john/www.john.} and wele to my {site/www.site.}.";
str = str.replace(/\{(.+?)\/(.+?)\}/g, function(m, label, url) {
return '<a href="http://' + url + '">' + label + '</a>';
});
document.write(str);
The regex is:
\{(.+?)\/(.+?)\}
\{
matches{
(.+?)
matches and captures anything (as few chars as possible, so up to the first/
)\/
matches/
(.+?)
matches and captures anything up to}
\}
matches}