删除标记名称和所有选择器但不包含内容

时间:2017-09-21 19:37:35

标签: javascript jquery html regex parsing

我有一个问题,我认为我会使用正则表达式,但如果有另一种方法,我很乐意听到它。

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

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

在显示结果之前,我需要将所有标记和选择器翻录并替换为<p>元素,但<li><ul>元素除外,这样它们就不会干扰任何内容当我重新显示它们但内容仍然存在且处于新的界限时。因此,在这种情况下,<div id="synopsis">This is text inside</div>将等于<p>This is text inside</p>

如果可能,我还要删除<ul><li>标记的所有属性,同时保留实际标记。

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

1 个答案:

答案 0 :(得分:1)

这应该可以解决问题。您首先需要将它添加到DOM和$(&#39; #eceopsis&#39;)。hide(),然后在处理之后,$(&#39; #etroopsis&#39;)。show()。< / p>

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

编辑:在\之后添加\ s