我对正则表达式不太满意。我很难找到一个小功能的解决方案。
我有一个ajax响应,它返回一个字符串,如“您的票证已成功记录。请点击链接查看详细信息123432。”
我所要做的就是使用javascript将该号码 123432 替换为<a href="blablabla.com?ticket=123432">
。
答案 0 :(得分:17)
试试这个:
fixedString = yourString.replace(/(\d+)/g,
"<a href='blablabla.com?ticket=$1\'>$1</a>");
这将为您提供一个如下所示的新字符串:
Your ticket has been successfully logged. Please follow the link to view details <a href='blablabla.com?ticket=123432'>123432</a>.
答案 1 :(得分:1)
var str = "Your ticket has been successfully logged. Please follow the link to view details 123432.";
str = str.replace(/\s+(\d+)\.$/g, '<a href="blablabla.com?ticket=$1">$&</a>');
此代码将输出
<a href="blablabla.com?ticket=123432">Your ticket has been successfully logged. Please follow the link to view details 123432.</a>