我需要一个JavaScript正则表达式用#{1}}标签包装“#hashtags”
示例:
之前:“我需要#help,请。#谢谢”
之后:“我需要<span>
#help <span>
,请。</span>
#Thanks <span>
”
目前,</span>
找到所有#hashtags,但如何将它们包装在span标签中?
谢谢!
答案 0 :(得分:17)
这有效 -
str = "I need #help, please. #Thanks"
str = str.replace(/(\#[a-zA-Z0-9\-\_]+)/g,"<span>$1</span>");
//> "I need <span>#help</span>, please. <span>#Thanks</span>"