1

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.

1
  • 1
    "This will return 4" - I don't understand. The numeric value 4 doesn't make sense, four elements doesn't make sense given that the selector 'a.mylink' won't match the html you've shown. What does $('a.mylink') have to do with the rest of your question? Are you saying the first code block is actually the content of a string and you want to change it to look like the second, or is that somewhere in your document's body, or...? Commented Jun 20, 2013 at 9:38

2 Answers 2

1

Use Javascript substr

var result = 
    val.substr(a, b);
Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

problem here is the youtube Url is not alw the same one, so we have to parse the url id for each case
You should store the youtube url in an element, then you can fetch in script. Check the above answer I've made changes in it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.