Alex Sokol Alex Sokol - 4 months ago 9
HTML Question

Javascript regEx to match spaces in string but ignore those in HTML tags?

Example string:

<div class="test">Hey </div> http://www.w3schools.com/html <a href="http://www.w3schools.com/html/">Visit our HTML tutorial</a> hello cool world


I need some regEx to split string by space get to result as:

['<div class="test">Hey </div>', 'http://www.w3schools.com/html', '<a href="http://www.w3schools.com/html/">Visit our HTML tutorial</a>', 'hello', 'cool', 'world']


Already have figured out some regEx, but it matches spaces between HTML tags.

\s(?=[^>]*(?:<|$))

Answer

Try the below solution.

var str = '<div class="test">Hey </div> http://www.w3schools.com/html <a href="http://www.w3schools.com/html/">Visit our HTML tutorial</a> hello cool world';
var pattern = /((<([^>]+)>+([^>]+)+<([^>]+)>)|\b([^><\s]+)\b)/g;
var result;

result = str.match(pattern);

console.log(result);