-2

I need to split this string by space:

<i>Lorem</i> 1 <span class="text-danger">ipsum</span> dolor sit amet, consectetur adipisicing elit. Animi consequuntur, eos? Error facere maiores minima molestiae obcaecati, quis voluptatum. Aspernatur cumque doloremque ducimus eos explicabo facilis fuga, nulla quos voluptate.

The difficulty is that I also have html tags inside.

I tried something like this, but it takes spaces as well

/<(.*?)>(.*?)<(.*?)>|\w*(.*?)(?:\s*?[,?.-])?/g

How do I solve this problem?

Emma
  • 1
  • 9
  • 28
  • 53
borkafight
  • 117
  • 1
  • 8
  • 4
    Do you also want to split `span class` or should it maintain together? – DongKingKong0 May 25 '19 at 16:04
  • 2
    What are you trying to accomplish? If you want to extract an element, use a selector or document parser. – ggorlen May 25 '19 at 16:36
  • 1
    Don't parse HTML with regex, [it's really a hard job](https://stackoverflow.com/a/4234491/372239), use a DOM parser instead. – Toto May 25 '19 at 17:00

1 Answers1

0

Here, we might not want to use regular expressions. But, if you really have to, I'm guessing that we could use space in a capturing group (\s) and than would likely work:

const regex = /(\s)/gm;
const str = `<i>Lorem</i> 1 <span class="text-danger">ipsum</span> dolor sit amet, consectetur adipisicing elit. Animi consequuntur, eos? Error facere maiores minima molestiae obcaecati, quis voluptatum. Aspernatur cumque doloremque ducimus eos explicabo facilis fuga, nulla quos voluptate.
`;
const subst = `\n`;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log('Substitution result: ', result);
Emma
  • 1
  • 9
  • 28
  • 53