我有一个 html 页面,现在我显示一个包含以“@”开头的文本的字符串。我需要做的是使用 javascript 替换以“@”开头的字符串中的所有文本与锚标记
E.g:
我的字符串:
与 @sarah333 和 @kevin0955 在海滩度过了愉快的时光。
所需输出:
had a great time with <a href="http://example.com/sarah333">@sarah333</a> and <a href="http://example.com/kevin9099">@kevin9099</a> at beach.
Try this
str = 'had a great time with @sarah333 and @kevin0955 at beach.';
replacedStr = str.replace(/\s\@(.*?)(\s|$)/g, ' <a href="http://example.com/$1">@$1</a>$2');
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)