使用jquery,匹配单个字符,后跟冒号并包装在span标签中

phirschybar

如果我有以下html:

<p>e: 1-800-hello-49</p>
<p>another line</p>
<p>
    f: 1-800-hello-49
    g: 1-800-hello-49
</p>

我希望能够在行首检测到一个字符,然后检测到一个冒号,以便获得以下输出:

<p><span>e:</span> 1-800-hello-49</p>
<p>another line</p>
<p>
    <span>f:</span> 1-800-hello-49
    <span>g:</span> 1-800-hello-49
</p>

我尝试过使用正则表达式,但是在这种情况下我很难掌握正则表达式。

阿伦·P·约翰尼(Arun P Johny)

尝试

$('p').html(function(idx, html){
    return html.replace(/\b([a-z]:)/ig, '<span>$1</span>')
})

演示:小提琴

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章