[英]replacing curly brackets and text in it with node
I have a string 我有一个字符串
var str="Hello my name is {john/www.john.com} and welcome to my {site/www.site.com}."
i have extracted curly brackets and made an anchor tag out of them like 我已经提取了大括号,并用它们制作了一个锚标记
<a href="www.john.com">john</a>
What i am trying to do is replace curly brackets and content in them with these nodes. 我想要做的是用这些节点替换大括号和内容。 Is it possible using regExp?
是否可以使用regExp? I have studied regExp on MDN but still cant figure out the way.
我已经研究过MDN上的regExp,但仍然无法弄清楚方法。
Sure it is: 当然是啦:
var str = "Hello my name is {john/www.john.com} and welcome to my {site/www.site.com}."; str = str.replace(/\\{(.+?)\\/(.+?)\\}/g, function(m, label, url) { return '<a href="http://' + url + '">' + label + '</a>'; }); document.write(str);
The regex is: 正则表达式是:
\{(.+?)\/(.+?)\}
\\{
matches {
\\{
matches {
(.+?)
matches and captures anything (as few chars as possible, so up to the first /
) (.+?)
匹配并捕获任何内容(尽可能少的字符,所以直到第一个/
) \\/
matches /
\\/
匹配/
(.+?)
matches and captures anything up to }
(.+?)
匹配并捕获任何东西到}
\\}
matches }
\\}
匹配}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.