4

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

Bojangles
  • 99,427
  • 50
  • 170
  • 208
James
  • 43
  • 2
  • 1
    Can you provide the output that you are expecting to match? From the question as it is right now, I can't understand what you are trying to extract. – mac Jul 21 '11 at 09:20
  • I'm trying to find a specific link(which the regex works for) within a normal text paragraph and .detach it and move it to another position, either within the same string or somewhere else. – James Jul 21 '11 at 09:25
  • Hi James. This doesn't apply to just you, but in future could you please leave tags out of the title - it's what the actual tags are for. – Bojangles Jul 21 '11 at 09:33

2 Answers2

1

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();
});
Michael Schinis
  • 697
  • 1
  • 7
  • 21
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.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295