首页 > 解决方案 > 正则表达式提取模板标签 <%= 和 %>

问题描述

我需要提取 <%= 和 %> 之间的所有内容,如下所示:

<%=你好%> <%=你好%> <%=你好%> <%=你好.bye%> <%=hello_you.bye_you%>

我有这段代码 - 我想捕获标签之间的所有内容,应该忽略空格。

["你好","你好","你好","hello.bye","hello_you.bye_you"]

我有这个尝试,有人可以改进正确的正则表达式以满足要求吗?

 var found = [], // an array to collect the strings that are found
 rxp = /<%=([^}}]+)%>/g,
 curMatch;

 while (curMatch = rxp.exec(str)) {
    found.push(curMatch[1]);
 }

标签: javascriptregex

解决方案


推荐阅读