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
Post a Comment