javascript - Split string with tags using regular expressions -


i split kind of texts in following manner:

"string1<%string2%>string3"   => [string1, <%string2%>, string3] "string1<%string2%><%string3%>"   => [string1, <%string2%>, <%string3%>] 

how can done regular expressions?

here way:

alert("string1<%string2%><%string3%>".split(/(<%.*?%>)/).filter(boolean));

the (<%.*?%>) regex matches <%...%>-like entities, , captures them preserved in resulting array. alternative regex can (<%(?!%>).*?%>).

with .filter(boolean) able rid of empty array elements.

in case there no spaces inside <%...%>, can use (<%\s+%>) regex.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -