我得到了以下“字符串”:
<form>{% include '../form.html' %}</form>
我现在想用javascript:
从import标签中提取“../form.html”var regex = /b{\% include | $\%}/i;
var source = "<form>{% include './form.html' %}</form>";
console.log(regex.exec(source));
然而我其实不知道该做什么^^。我的问题是:
"{"
和"%"
)"'"
和"'"
更新
如果我有一个包含多个外观的整个文本呢?
<form>{% include '../form.html' %}</form>
<table>{% include '../table.html' %}</table>
答案 0 :(得分:2)
没有什么特别的事情要做:
var regex = /{% (?:include|import|extends) '([^']+)' %}/gi;
var source = "<form>{% include './form.html' %} {% import './form2.html' %}</form>";
var results = [], found;
while (found = regex.exec(source)) {
results.push(found[1]);
}
console.log(results);
答案 1 :(得分:1)
试试这个:
\{% include '(.+?)' %\}
通过使用非贪婪的.+?
并将其捕获,您的第一个捕获组将包含您想要的./form.html
。
答案 2 :(得分:1)
您可以使用:
s="<form>{% include '../form.html' %}</form>";
re = /\{% +include +(['"])([^'"]+)\1/;
fn = s.match(re)[2]; //=> ../form.html
答案 3 :(得分:1)
你可以这样做:
var text = "<form>{% include '../form.html' %}</form>";
var extractedUrl = /(?:include '(.*?)' %)/.exec(text)[1];
答案 4 :(得分:1)
您可以按照输入模式,整个匹配在组0中 引号之间的部分是第1组。
# /<form>{%[ \t]+include[ \t]+'([^']+)'[ \t]+%}<\/form>/
<form>{% [ \t]+ include [ \t]+
'
( [^']+ ) # (1)
'
[ \t]+ %}</form>