用锚标记替换以@开头的文本 [英] Replace text starting with @ with anchor tag
本文介绍了用锚标记替换以@开头的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个html页面,现在我显示一个包含以'@'开头的文本的字符串.我需要做的是使用javascript之类的标记将所有以'@'开头的字符串中的所有文本替换为锚标记,
I have a html page, Now I am displaying a string which contains text starting with '@'. What I need to do is to replace all texts in a string starting with '@' with anchor tag using javascript something like
例如:
我的字符串:
与@ sarah333和@ kevin0955在海滩度过了愉快的时光.
had a great time with @sarah333 and @kevin0955 at beach.
必需的输出:
had a great time with <a href="http://example.com/sarah333">@sarah333</a> and <a href="http://example.com/kevin9099">@kevin9099</a> at beach.
推荐答案
尝试一下
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');
这篇关于用锚标记替换以@开头的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文