繁体   English   中英

用javascript中的html标记替换

[英]Replace with html tag in javascript

我想替换输入中的任何此类文本: [www.Link-to-be-shortened.com]following link(cut)

我想用<a href="cuer.esy.es/?f=www.Link-to-be-shortened.com">following link</a>将其替换为javascript

我已经尝试过此代码:

var UserString = " Hi <br>This article is good , Please visit the following link [www.Link-to-be-shortened.com]following link(cut)";
var SystemString = UserString.replace("[", "");
SystemString = SystemString.replace("]following link(cut)", "");
var a = document.createElement('a');
var linkText = document.createTextNode("following link");
a.appendChild(linkText);
a.title = "following link";
a.href = "http://cuer.esy.es/?f="+SystemString;
document.body.appendChild(a);

但是此代码无法正常工作

这是一个如何使用正则表达式执行此操作的简单示例:

 var UserString = "[www.Link-to-be-shortened.com]Click here(cut)"; var link = UserString.replace(/\\[([^\\[]+)\\]([^(]+)\\(cut\\)/g, '<a href="cuer.esy.es/?f=$1">$2</a>'); console.log(link); 

但是 ,这并非在所有可能的情况下都有效。 如果只有受信任的人正在提交链接,则可以使用此选项。

暂无
暂无

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

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