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,597
  • 2
  • 20
  • 46

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
  • 21,926
  • 7
  • 38
  • 60
0

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

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

console.log(c);
varoons
  • 3,807
  • 1
  • 16
  • 20