I would like to split a string containing HTML snippets in characters and the elements.
let str = 'wel<span class="t">come</span> to all'
console.log("split attempt " +JSON.stringify(str.split(/(<.*?>)|/)));
giving me:
split attempt ["w",null,"e",null,"l","<span class=\"t\">","c",null,"o",null,"m",null,"e","</span>"," ",null,"t",null,"o",null," ",null,"a",null,"l",null,"l"]
By filtering out the null, I get what I want:
split attempt ["w","e","l","<span class=\"t\">","c","o","m","e","</span>"," ","t","o"," ","a","l","l"]
But is there some way in the regular expression to filter out specific sequences (like short HTML tags) and split the rest in the vanilla character by character way?
str.split(/(<[^>]+>|)/).filter(Boolean)but keep in mind that this will work for simple non-nested tags only.<and>in other chars likeKspan class="t"D. And mentally ignore the problem that the capital K & D cannot be both present.matchlike @trincot suggest with<[^>]*>|[^><]- negation can be more efficient, if this works with your data.