我正在尝试查找一些文本 RegEx /\w*http:[/][/]bit.ly[/]\w*/ig
这将找到该字符串并将其拉出,将其移动到 <span>
标签,或 <p>
的末尾标签?
<p class="regex">Text before http://bit.ly/wtGAhsu sometext here, doesn't matter how long this is.... <span></span></p>
$("p:regex('(\w*http:[/][/]bit.ly[/]\w*)')").addClass('active');
以上是我到目前为止所拥有的(仅选择p),我已经尝试过.highlight()
但我现在不确定是否要抓取文本并移动它,非常感谢任何帮助。
谢谢
最佳答案
你可以使用这样的东西:
$("p.regex").each ( function () {
var jThis = $(this);
var newSrc = jThis.text ().replace (/^(.*)(https?\:\/\/bit\.ly\/\w+)(.*)$/i, '$1$3<span>$2</span>');
jThis.html (newSrc);
} );
请注意,此版本假定每个段落最多有一个链接。
关于jquery - 在字符串中查找文本并添加到其他位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6773799/