RegEx matching space not starting/ending with char
I used /(?!&)\s(?!&)/g. But that ignore space with & character only at end.
If you cannot split (and you cannot split since there is no look-behind in JS), you need to match:
var re = /(\S+(?:\s&\s\S+)*)/g; var str = 'H & M shopping mall'; var m = str.match(re); document.getElementById("res").innerHTML = m;
You can use look ahead and look behind... (just add < to your current regex)
- All spaces whose previous character is not &.. (?<!&) and next character is also not &.. (?!&)