正则表达式用标签包装文本

时间:2014-02-08 20:09:32

标签: javascript regex

我需要一个JavaScript正则表达式用#{1}}标签包装“#hashtags”

示例:

之前:“我需要#help,请。#谢谢”

之后:“我需要<span>#help <span>,请。</span>#Thanks <span>

目前,</span>找到所有#hashtags,但如何将它们包装在span标签中?

谢谢!

1 个答案:

答案 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>"