【问题标题】:Removing tag name and all selectors but not content删除标签名称和所有选择器,但不删除内容
【发布时间】:2017-09-21 19:37:35
【问题描述】:

我有一个问题,我认为我会使用正则表达式,但如果有其他解决方法,我很想听听。

我的问题是我从网站上抓取产品的描述。现在要做到这一点,我正在使用类似:description= $('.description').html();。这从网站获得了我需要的所有内容,在这种情况下,是百思买的命运 2。结果就是 sn-p 中的内容。

<div id="synopsis">From the makers of the acclaimed hit game Destiny, comes the much-anticipated sequel. An action shooter that takes you on an epic journey across the solar system.<br><br>Humanity’s last safe city has fallen to an overwhelming invasion force, led by Ghaul, the imposing commander of the brutal Red Legion. He has stripped the city’s Guardians of their power, and forced the survivors to flee. You will venture to mysterious, unexplored worlds of our solar system to discover an arsenal of weapons and devastating new combat abilities. To defeat the Red Legion and confront Ghaul, you must reunite humanity’s scattered heroes, stand together, and fight back to reclaim our home.</div>
<div id="features"><div class="icon-feature-list"></div><div class="feature"><span class="type-paragraph-title">Includes: Destiny 2 Base Game</span><p></p></div><div class="feature"><span class="type-paragraph-title">Gameplay Features:</span><p>- Rich cinematic story campaign.</p></div><div class="feature"><p>- Multiple cooperative game modes for epic, social fun.</p></div><div class="feature"><p>- Intense 4v4 competitive multiplayer matches, including 5 different PVP modes.</p></div><div class="feature"><p>- Expansive, never-before-seen worlds and spaces to explore.</p></div><div class="feature"><p>- Customize your character’s weapons and armor with an all-new array of gear.</p></div><div class="feature"><p>- Discover Lost Sectors, complete new Adventure missions, or rally to Public Events with other Guardians.</p></div><div class="feature"><p>- Introducing a brand new Guided Games system that helps players find like-minded groups to experience Destiny 2’s most challenging activities, like the Raid.</p></div></div>

在显示结果之前,我需要删除所有标签和选择器并替换为 &lt;p&gt; 元素,&lt;li&gt;&lt;ul&gt; 元素除外,这样当我重新显示它们时它们不会干扰任何东西,但内容仍然存在并且在一个新的行上。所以在这种情况下&lt;div id="synopsis"&gt;This is text inside&lt;/div&gt; 将等于&lt;p&gt;This is text inside&lt;/p&gt;

如果可能,我还想删除 &lt;ul&gt;&lt;li&gt; 标签的所有属性,同时保留实际标签。

希望这是有道理的,我感谢任何人可以给我的任何帮助,如果有其他我没有想到的解决方案,我很想听听。

【问题讨论】:

  • stringOfHTML.replace(/&lt;\/?[^&gt;]+(&gt;|$)/g, "&lt;p&gt;")
  • 只适用于

    .

  • 试试这个str.replace(/((id=)".+")|(class=".*")/,"");
  • 出于某种原因 Sayam,这取出了除了最后一个 li 元素之外的大部分数据。我还需要删除的不仅仅是类和 id。许多网站都有我不想在我的网站上出现的“aria-label”或“data-recommendations”等属性。基本上 中的任何内容都需要替换为 p 标签。

标签: javascript jquery html regex parsing


【解决方案1】:

这应该可以解决问题。您首先需要将其添加到 DOM 和 $('#synopsis').hide(),然后在处理后,$('#synopsis').show()。

    // Remove classes and IDs 
    $('#synopsis').find('*').removeAttr('class').removeAttr('id');

   // Convert all tags to <p> tags except UL, LI
    $('#synopsis').find('*').not('ul, li').replaceWith(function() {
        return $('<p/>', {
            html: this.innerHTML
        });
    });

如果您无法将其添加到 DOM,您将需要您建议的正则表达式。

// Convert all tag beginnings (e.g. <div) except ul, li to p
str.replace(/<(?!ul|li\b)\b\w+/g  , '<p');

// convert all tag endings (e.g. /div>) except ul, li to p
str.replace(/\/((?!ul|li)\w+)>/g , '/p>');

// Remove all classes, ids etc from resulting <p> tags
str.replace(/<p\s[^>]+>/g , '<p>');

编辑:在

【讨论】:

  • 有没有办法在不将其添加到 DOM 的情况下做到这一点?我正在使用 Chrome 扩展程序,需要将结果传输到我的网站。另外我正在寻找可以删除所有属性的东西。不仅仅是班级和身份证。许多网站都有我不想在我的网站上出现的“aria-label”或“data-recommendations”等属性。
  • 太棒了!奇迹般有效。非常感谢!
  • 好吧,我认为它最初可以工作,但数据在浏览器中看起来非常古怪。随着

    这种事情经常发生。我认为这是因为您不应该使用正则表达式解析 HTML。您能想到的任何解决方法?

  • 再次编辑,对不起,我忘记了 \s 在最后一个正则表达式中导致误报。现在就试试。顺便说一句,您可以使用正则表达式完全解析 html,这就是(某些)HTML 解析器的工作方式。如果你想测试一个正则表达式,你可以把它弹出到这个站点:regex101.com
  • 原始源代码中可能有空的

    标记,因为许多 HMTL 编辑器添加它们用于格式化。您可能希望将您的结果与来源进行比较,看看是否是这种情况。
猜你喜欢
  • 2012-04-08
  • 2013-05-02
  • 1970-01-01
  • 1970-01-01
  • 2013-07-02
  • 1970-01-01
  • 2012-11-12
  • 2011-06-08
  • 2013-12-03
相关资源
最近更新 更多