我想在输入中替换任何这样的文本: [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);
但是,这并非在所有可能的情况下都有效。如果只有受信任的人正在提交链接,则可以使用此选项。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句