[英]Replace words with html elements
I am working on a Twitter program and am stuck on how to replace words with html elements. 我正在开发一个Twitter程序,并坚持如何用html元素替换单词。 For example: 例如:
Replace: Hello #World, This is a Tweet!
替换: Hello #World, This is a Tweet!
With: Hello <a href="">#World</a>, this is a Tweet!
随着: Hello <a href="">#World</a>, this is a Tweet!
I know that you can use regex in replace()
but I am unsure of how to reference to the matched string after in the second argument of replace()
我知道,你可以使用正则表达式的replace()
但我不确定如何在第二个参数参考匹配的字符串后replace()
Thank you! 谢谢!
You can use this regular expression 您可以使用此正则表达式
str="Hello #World"
str.replace(/#(\S*)/g,'<a href="">$1</a>');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.