0

I am trying to split a string by matching some patterns using regex, for instance i have <span>Hello World</span> and the result would be ["<span>", "Hello World", "</span>"]

// Tried this
console.log(arr.split(/(<*>)/));
// and this:
console.log(arr.split(/(^<$>)/));
Sachihiro
  • 1,252
  • 2
  • 15
  • 34

2 Answers2

1

You can do something like

  const s = `<span>Hello World</span>`; 
  const output = s.split(/(<\/?span>)/g).filter(Boolean);

  console.log(output);
Zohaib Ijaz
  • 19,906
  • 6
  • 35
  • 55
0

const a = `<span>Hello World</span>`; 

var c = a.split(/^(<.*>)(.*?)(<.*?>)$/g).filter(x => x);

console.log(c);
varoons
  • 3,383
  • 1
  • 13
  • 18