0

I would like to remove anchor tag for "check out the slideshow". Check out the slideshow text can change. So, we have to identify with #show-gallery from the href.

var jStr = $('.gallery-wrapper').html();
cStr = jStr.Replace('/#show-gallery/','');
<div class='gallery-wrapper'><div class='knot-slideshow' data-slideshow-slug='oatmeal-with-kids-0' data-slideshow-id='266039'></div><ul class='knot-slideshow-data' style='display:none;'></ul></div><br><br /><p>You know the song "Me and My Shadow"? Well that is exactly the tune that comes to mind whenever I watch Mike and Noah. Literally.</p> <p>If Mike is around you can be sure that Noah is right there. Next to him. At all times. And it doesn't matter what Mike is doing. Shoveling, putting the recycling together - whatever the situation, Noah is there to "help".</p> <p>He adores his daddy, and wants to do everything Daddy does. And while this is not always so convenient for Mike (as a 4-year-old's idea of "help" is questionable, at best), it does come in handy at breakfast. Noah is delighted when Mike can join us for breakfast, and wants to have whatever he is having.</p> <p>Fortunately, Mike is a very healthy eater, and often has a big bowl of oatmeal in the morning. So I was delighted to have the opportunity to sample <a href="http://www.quakeroats.com/products/hot-cereals/perfect-portions/maple.aspx">Quaker Perfect Portions</a>.</p> <p><a href="http://www.kitchendaily.com/read/eating-oatmeal-with-kids/#show-gallery">Check out the slideshow above</a> to learn more about <a href="http://sherisilver.com/2013/05/17/like-father-like-son-sponsored-post/">Donuts, Dresses And Dirt</a>'s oatmeal breakfast!</p> <p>Disclosure: Compensation and product for this post was provided by Quaker, via Media. All opinions expressed here - as always - are completely my own, and are not indicative of the opinions or positions of Quaker.<br /> </p>

So my final snippet should look like

<div class='gallery-wrapper'><div class='knot-slideshow' data-slideshow-slug='oatmeal-with-kids-0' data-slideshow-id='266039'></div><ul class='knot-slideshow-data' style='display:none;'></ul></div><br><br /><p>You know the song "Me and My Shadow"? Well that is exactly the tune that comes to mind whenever I watch Mike and Noah. Literally.</p> <p>If Mike is around you can be sure that Noah is right there. Next to him. At all times. And it doesn't matter what Mike is doing. Shoveling, putting the recycling together - whatever the situation, Noah is there to "help".</p> <p>He adores his daddy, and wants to do everything Daddy does. And while this is not always so convenient for Mike (as a 4-year-old's idea of "help" is questionable, at best), it does come in handy at breakfast. Noah is delighted when Mike can join us for breakfast, and wants to have whatever he is having.</p> <p>Fortunately, Mike is a very healthy eater, and often has a big bowl of oatmeal in the morning. So I was delighted to have the opportunity to sample <a href="http://www.quakeroats.com/products/hot-cereals/perfect-portions/maple.aspx">Quaker Perfect Portions</a>.</p> <p>Check out the slideshow above to learn more about <a href="http://sherisilver.com/2013/05/17/like-father-like-son-sponsored-post/">Donuts, Dresses And Dirt</a>'s oatmeal breakfast!</p> <p>Disclosure: Compensation and product for this post was provided by Quaker, via Media. All opinions expressed here - as always - are completely my own, and are not indicative of the opinions or positions of Quaker.<br /> </p>
4
  • Name of the method is replace not Replace. JavaScript is case-sensitive. Also you should select the target a element and modify it's property instead of resetting entire HTML content. Commented Feb 13, 2015 at 22:07
  • Do you want someone to write a regular expression for you, or have I misunderstood your question? Commented Feb 13, 2015 at 22:13
  • Yes, I need help with regex. to replace the <a href='url/#show-gallery'>any text</a> Commented Feb 13, 2015 at 22:49
  • Have you tried writing one yet? Commented Feb 13, 2015 at 23:43

2 Answers 2

3

Try this way to replace your href with #show-gallery to your html text, See DEMO

var re = /<a\s.*?href=["']([^"']*?#show-gallery[^"']*?)[^>]*>(.*?)<\/a>/g;
var str = '<div class=\'gallery-wrapper\'><div class=\'knot-slideshow\' data-slideshow-slug=\'oatmeal-with-kids-0\' data-slideshow-id=\'266039\'></div>\n<ul class=\'knot-slideshow-data\' style=\'display:none;\'></ul></div><br><br />\n<p>You know the song "Me and My Shadow"? Well that is exactly the tune that comes to mind whenever I watch Mike and Noah. Literally.\n</p>\n <p>If Mike is around you can be sure that Noah is right there. Next to him. At all times.\n And it doesn\'t matter what Mike is doing. Shoveling, putting the recycling together - whatever the situation, Noah is there to "help".</p> \n <p>He adores his daddy, and wants to do everything Daddy does. And while this is not always so convenient for \n Mike (as a 4-year-old\'s idea of "help" is questionable, at best), it does come in handy at breakfast. Noah is delighted \n when Mike can join us for breakfast, and wants to have whatever he is having.</p> <p>Fortunately, Mike is a very healthy eater, \n and often has a big bowl of oatmeal in the morning. So I was delighted to have the opportunity to sample \n <a href="http://www.quakeroats.com/products/hot-cereals/perfect-portions/maple.aspx">Quaker Perfect Portions</a>.</p> \n <p><a href="http://www.kitchendaily.com/read/eating-oatmeal-with-kids/#show-gallery">Check out the slideshow above</a>\n to learn more about <a href="http://sherisilver.com/2013/05/17/like-father-like-son-sponsored-post/">Donuts, Dresses And Dirt</a>\'\n s oatmeal breakfast!</p> <p>Disclosure: Compensation and product for this post was provided by Quaker, via Media. \n All opinions expressed here - as always - are completely my own, and are not indicative of the opinions or positions of Quaker.<br /> </p>';
var subst = '$2';

var result = str.replace(re, subst);

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

Here's my example of this too - as yes, it is more generic, but it's a very simple and needed tool. Thanks to @Curious_Mind for starting me off on the right foot. Fortunately this works well in ionic project.

// Just exports the URL as text - with no link.

var stringText = '<a href="http://helloworld.com">Cool link</a> is a great site';
var stringText = striplinks(stringText);

document.getElementById("test").innerHTML = stringText;

// string will be: Cool link is a great site

function striplinks(text) {
  var re = /<a\s.*?href=[\"\'](.*?)[\"\']*?>(.*?)<\/a>/g;
  var str = text;
  var subst = '$2';
  var result = str.replace(re, subst);
  return result;
}
<p>This is a full JS example.</p>
<div id="test">
  </test>

This will change a string of text link from (for example):

<a href="http://helloworld.com">Cool link</a>

to

helloworld.com

Now, to the code (for ionic angular - the above inline example is for pure JS):

// Just exports the URL as text - with no link.
  var stringText = "<a href="http://helloworld.com">Cool link</a> is a great site";
  var stringText = striplinks(stringText );

 // string will be: Cool link is a great site

  striplinks(text) 
  { 
    var re = /<a\s.*?href=[\"\'](.*?)[\"\']*?>(.*?)<\/a>/g;
    var str = text; 
    var subst = '$2';
    var result = str.replace(re, subst);
    return result;
  }

Comments

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.