bugfix> javascript > 投稿

HTMLコードのすべてのマーカーを削除する次の関数があります。

function saringtags(r, l) {
    for (var e = r.split("<"), n = 0; n < e.length; n++) - 1 != e[n].indexOf(">") && (e[n] = e[n].substring(e[n].indexOf(">") + 1, e[n].length));
    return e = e.join(""), e = e.substring(0, l - 1)
}

問題はこれです:

<div class="lyric">
<blockquote>
It's nine o'clock on a Saturday<br>
The regular crowd shuffles in<br>
There's an old man sitting next to me<br>
Makin' love to his tonic and gin<br>
</blockquote>
</div>

これに変わります:

It's nine o'clock on a SaturdayThe regular crowd shuffles inThere's an old man sitting next to meMakin' love to his tonic and gin

の例外を作成する方法 <br> 関数内のマーカー? この結果は私が必要なものです:

It's nine o'clock on a Saturday<br>The regular crowd shuffles in<br>There's an old man sitting next to me<br>Makin' love to his tonic and gin

回答 3 件
  • 正規表現を使用:

    /<.*[^br]>/g

    html.replace(/<.*[^br]>/g,'')
    
    

    https://regexr.com/58l77

  • 再実装しました innerText

    console.log(document.getElementsByClassName('lyric')[0].innerText)
    
    
    <div class="lyric">
    <blockquote>
    It's nine o'clock on a Saturday<br>
    The regular crowd shuffles in<br>
    There's an old man sitting next to me<br>
    Makin' love to his tonic and gin<br>
    </blockquote>
    </div>
    
    

    (本当に必要な場合 <br> 改行文字を置き換えることができるタグ <br> その後。これは、正規表現やその他の文字列操作方法でHTMLを操作しようとするよりも、エラーが発生しにくくなります。)

  • お役に立てば幸いです

    var text = `<div class="lyric"><blockquote>It's nine o'clock on a Saturday<br>The regular crowd shuffles in<br>There's an old man sitting next to me<br>Makin' love to his tonic and gin<br></blockquote></div>"`;
    var str = text.replace(/<br>/gi, ' vvvvv ').replace(/<\/?[^>]+(>|$)/g, "").replace(/ vvvvv /gi, '<br>');
    console.log(str);
    
    

あなたの答え