-1

I'm looking for a solution similar to Regex to replace multiple spaces with a single space but instead of space the question is about <span>. It doesn't contain additional attributes in it such as class. It's just exactly 6 symbols <span> (no spaces, no nothing).

As result, the string

"<span>The <span><span><span><span>dog <span><span>has</span> a long</span> tail, and it </span></span></span>is RED</span></span>!"

should be replaced to

"<span>The <span>dog <span>has</span> a long</span> tail, and it </span></span></span>is RED</span><span>!"

(please don't pay attention closing spans will be more, additional modifications are expected thereafter).

P.S. Yes, you're right, you may want to ask if 2+ consequent spans may have spaces in between, tabs or even new lines. Honestly - yes, but even without spaces, tabs, new lines the answer will be useful. Thank you.

Haradzieniec
  • 9,086
  • 31
  • 117
  • 212

1 Answers1

-1

Try out the following two replace methods (can you use them chained): if or is repeated directly after another (twice or more often), replace that whole thing with just one expression:

.replace(/(\<span\>){2,}/g, "<span>")
.replace(/(\</span\>){2,}/g, "</span>")

By the way, regexr.com is a great place if you want to try out regex!

Lorenz Mueller
  • 140
  • 3
  • 11