问题
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