Replace multiple
's with only one

前端 未结 9 2098
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-01 12:55

How do I use JavaScript to detect




to become one


?

I trie

9条回答
  •  生来不讨喜
    2021-02-01 13:32

    A lot of the other answers to this question will only replace up to certain amount of elements, or use complex loops. I came up with a simple regex that can be used to replace any number of
    tags with a single tag. This works with multiple instances of multiple tags in a string.

    /(
    *)+/g

    To implement this in JavaScript, you can use the String.replace method:

    myString.replace(/(
    *)+/g, "
    ");

    To replace multiple
    tags, add a / to the regex:

    /(*)+/g
    

提交回复
热议问题