Find text in a string and prependTo somewhere else

心不动则不痛 提交于 2019-12-10 17:50:38

问题


I am trying to find some text RegEx /\w*http:[/][/]bit.ly[/]\w*/ig that will find this string and pull it out, moving it to the <span> tag, or at the end of the <p> tag?

<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');

Above is what I have so far (just selecting the p), I've tried .highlight() but Im not sure now to grab the text and move it, any help is much appreciated.

Thanks


回答1:


You could use something like this:

$("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);
} );

Note that this version assumes one link, max, per paragraph.

See it in action at jsFiddle.




回答2:


Try the following:

HTML:

<div id="moveToArea"></div>

Javascript:

$("p:regex('(\w*http:[/][/]bit.ly[/]\w*)')").addClass('active');
var item = $(".active")
$("$moveToArea").append(item, function(){
item.fadeOut();
});


来源:https://stackoverflow.com/questions/6773799/find-text-in-a-string-and-prependto-somewhere-else

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!