0

Hi guys thanks for pay attention to this. Well im trying to replace every full youtube url with a word. An Example:

http://www.youtube.com/watch?v=lDC0gdrcauI Should be replaced with the word "Video"
http://www.youtube.com/watch?v=ef0wk8QoA34&feature=g-all-u Should be replaced with the word "Video"

Here is my code thus far. HTML:

<p>My brother is great.</p>
<p>http://www.youtube.com/watch?v=ef0wk8QoA34</p>

​ And JS:

(function($) {

    $(function(){
    var thePage = $("body");
    thePage.html(thePage.html().replace(/My brother/g, 'My Pig').replace(/youtube.com/g, 'video'));
    });

})(jQuery)

​A JSFiddle can be found here: http://jsfiddle.net/QmRc7/2/

So if you could help me with that ill be great! Thanks!

PS: Sorry guys my post was edited not by me :/

Solved! I just add this and works great +1 for everyone!:

.replace(/(www.)?youtu(be\.com|\.be)\/(watch\?v=)?([A-Za-z0-9._%-]*)(\&\S+)?/g, '')
fecapeluda
  • 133
  • 1
  • 6
  • 20

3 Answers3

3

If I understand your question correctly (from it's title), you want to filter anchor tags using regex on their content(text):

You can do it this way:

$('a').filter(function() {
    return $(this).text().match(/^(?:https?:\/\/)?(?:www\.)?youtube\.com\/watch\?(?=.*v=((\w|-){11}))(?:\S+)?$/g);
}).each(function() {
    $(this).text($(this).text().replace(/youtube.com/g, 'video'));
});

​ the code basically, selects anchor tags with text matching youtube url pattern which I copied from this answer and for each matching anchor tag, replaces youtube.com with video.


Answer to updated question:

you can do something like this:

$('a').filter(function() {
    return $(this).text().match(/^(?:https?:\/\/)?(?:www\.)?youtube\.com\/watch\?(?=.*v=((\w|-){11}))(?:\S+)?$/g);
}).each(function() {
    $(this).text('Video');
});

Note that it doesn't match non anchor tags, you can use $('*') instead of $('a') to select all nodes.


And the second word change can be done easily in this way:

$('p').each(function() {
    $(this).text($(this).text().replace(/My brother/g, 'My Pig'));
});

Needless to say, you should change the selectors to match your desired nodes.

Community
  • 1
  • 1
fardjad
  • 20,031
  • 6
  • 53
  • 68
  • 1
    I created the question since it was causing issues. The OP did clarify, as well as the example that was provided, the Youtube URL is a text string in `

    ` tags. That said, using `$('p')` as the selector should be the same. Please feel free to edit the question if you have a cleaner version to use, as this is a unique requirement for the OP. Thanks.

    – arttronics Jun 12 '12 at 04:28
  • 1
    **+1** to include `.filter` along with `.each`. The only thing missing is to include the 2nd word change for the 2nd $('p') that's before the first one. – arttronics Jun 12 '12 at 04:31
  • @arttronics I'd rather not to change the OP question as it's still not clear and any edits may change it completely. – fardjad Jun 12 '12 at 04:52
  • To clarify: No `anchors` are used. The Youtube link is a text string as seen in the jsFiddle that the OP provided. That said, it's in the `

    ` tags that you can plainly see.

    – arttronics Jun 12 '12 at 04:57
  • @fardjad Thanks guys! I edit my post so i wanna ask if it's possible to "hide" every url related to youtube (no HTML link refered just text)?. – fecapeluda Jun 12 '12 at 12:50
1

This regex might need a bit more tweaking to get all the possible youtube URL patterns, but this would be a good start:

http:\/\/www\.youtube\.com\/[\w?=&-]+
Mark Lybrand
  • 245
  • 4
  • 13
0

After few hours ... I finally got solved this with your help obviously.

.replace(/(www.)?youtu(be\.com|\.be)\/(watch\?v=)?([A-Za-z0-9._%-]*)(\&\S+)?/g, '')
fecapeluda
  • 133
  • 1
  • 6
  • 20