0

I have the following string:

var x = '<p>Hello there <span contenteditable="false" class="fr-deletable">aaa</span>,<br></p><p>Hi <span contenteditable="false" class="fr-deletable">bbbb</span>,<br></p>';

I basically want to strip out all of the <span></span> tags but keep the inner content in tact. So the above should turn into:

'<p>Hello there aaa,<br></p><p>Hi bbbb,<br></p>';

I've tried a few things:

x.replace(/<span.*>(.*)<\/span>/g, '$1');
x.replace(/<span.*>/g, '');

But none of these gets me what I need. Help please?

7ball
  • 2,183
  • 4
  • 26
  • 61

3 Answers3

1

const input = '<p>Hello there <span contenteditable="false" class="fr-deletable">aaa</span>,<br></p><p>Hi <span contenteditable="false" class="fr-deletable">bbbb</span>,<br></p>';
const output = input.replace(/<span [^>]+>([^<]+)<\/span>/g, '$1');
console.log(output);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
0

It's better to do after parsing the HTML, which can be done using jQuery.

var x = '<p>Hello there <span contenteditable="false" class="fr-deletable">aaa</span>,<br></p><p>Hi <span contenteditable="false" class="fr-deletable">bbbb</span>,<br></p>';

console.log(
  $('<div>', {
    html: x
  }).find('span').replaceWith(function() {
    return this.innerHTML;
  }).end().html()
)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

FYI : Using regular expressions to parse HTML: why not?

Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
0

Try this:

var x = '<p>Hello there <span contenteditable="false" class="fr-deletable">aaa</span>,<br></p><p>Hi <span contenteditable="false" class="fr-deletable">bbbb</span>,<br></p>';
var y = x.replace(/(<sp|<\/sp)[^>]+>/g, "");
console.log(y);
Mohammed Elhag
  • 4,272
  • 1
  • 10
  • 18