正则表达式标识字符串并删除该字符串的href

时间:2015-02-13 22:05:04

标签: javascript jquery html regex

我想删除&#34的锚标记;查看幻灯片"。查看幻灯片文字可以更改。因此,我们必须使用href中的#show-gallery进行识别。



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>
&#13;
&#13;
&#13;

所以我的最终片段应该是

<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>

2 个答案:

答案 0 :(得分:2)

尝试这种方式将href替换为#show-gallery html textDEMO

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

答案 1 :(得分:0)

这也是我的示例-是的,它更通用,但这是一个非常简单且需要的工具。感谢@Curious_Mind让我从右脚开始。幸运的是,这在离子项目中效果很好。

// 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>

例如,这将更改文本链接的字符串:

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

to

helloworld.com

现在,到代码了(对于离子角-上面的内联示例是针对纯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;
  }