【问题标题】:Regex to identify a string and to remove href of that string正则表达式来识别字符串并删除该字符串的 href
【发布时间】:2015-02-13 22:05:03
【问题描述】:

我想删除“查看幻灯片”的锚标记。查看幻灯片文本可以更改。因此,我们必须从 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>

所以我最终的 sn-p 应该是这样的

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

【问题讨论】:

  • 方法的名称是replace 而不是Replace。 JavaScript 区分大小写。此外,您应该选择目标 a 元素并修改它的属性,而不是重置整个 HTML 内容。
  • 你是想让别人为你写一个正则表达式,还是我误解了你的问题?
  • 是的,我需要正则表达式方面的帮助。替换 任何文本
  • 你试过写一个了吗?

标签: javascript jquery html regex


【解决方案1】:

尝试用这种方式将您的href 替换为#show-gallery 到您的html text,请参阅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);

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2013-06-07
      • 1970-01-01
      • 2021-05-31
      • 2014-11-07
      • 1970-01-01
      • 2011-06-28
      • 1970-01-01
      • 2021-09-07
      • 1970-01-01
      相关资源
      最近更新 更多