繁体   English   中英

JavaScript regexr用html替换字符串?

[英]JavaScript regexr replace string with html?

下面是我的代码。

var input = "this is @renish profile";
var matches = input.replace(/(@\w*)/g,'<a href="http://example.com/users/profile/$1">$1</a>');

输出为:

this is <a href="http://example.com/users/profile/@renish">@renish</a> profile

这是打印html代码。 如何从字符串转换标签?

创建一个div,然后使用innerHTML。

 var input = "this is @renish profile"; var matches = input.replace(/(@\\w*)/g,'<a href="http://example.com/users/profile/$1">$1</a>'); var div = document.createElement('div'); div.innerHTML = matches; document.body.appendChild(div) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM