Nonyck
Nonyck

Reputation: 682

Javascript replace from to

I found good tutorials on how to do a string replace all for a string, but how to replace from a to b ?

Lets say we have this selector

$('a.mylink')

This selector will return something like

<a class=​"micro_avatar my_avatar">​</a>​ 
<a href=​"#!/​users/​user_name" style=​"background:​ 
  url(https:​/​/​secure.gravatar.com/​avatar/​randomnumber?size=24&default=mm)​ 
  no-repeat" class=​"micro_avatar">​</a>​
http://www.youtube.com/watch?v=_sgu7CioctU

In the page that Im testing this, will find multiple ocurrences of this w variations ( imagine a blog and each post have this inside).

Now we want to replace all the ocurrences to look like this

<a class=​"micro_avatar my_avatar">​</a>​ 
<a ** rel='nofollow' ** href=​"#!/​users/​user_name" style=​"background:​ 
  url(https:​/​/​secure.gravatar.com/​avatar/​randomnumber?size=24&default=mm)​ 
  no-repeat" class=​"micro_avatar">​</a>​

**<iframe width="560" height="315" src="** http://www.youtube.com/embed/_sgu7CioctU
**" frameborder="0" allowfullscreen></iframe>**

The trick part is not all the youtube links will have the same link.

Upvotes: 1

Views: 74

Answers (2)

Rohan Kumar
Rohan Kumar

Reputation: 40639

Try this,

Updated

HTML

<span class="youtube_url">http://www.youtube.com/embed/_sgu7CioctU</span>

SCRIPT

$('.micro_avatar:not(.my_avatar)').attr('rel',nofollow);
$('<iframe>', {
   src: $('.youtube_url').text(),
   id:  'myFrame',
   width: 560,
   allowfullscreen: true,
   height:350
}).appendTo('body');

Upvotes: 1

Suresh Atta
Suresh Atta

Reputation: 122026

Use Javascript substr

var result = 
    val.substr(a, b);

Upvotes: 1

Related Questions