[英]How can I extract all characters between two delimiters?
I got following "string": 我得到了“字符串”:
<form>{% include '../form.html' %}</form>
I would now like to extract the "../form.html" out of the import tag with javascript: 我现在想使用javascript从导入标记中提取“ ../form.html”:
var regex = /b{\% include | $\%}/i;
var source = "<form>{% include './form.html' %}</form>";
console.log(regex.exec(source));
However I actually have no idea what to do ^^. 但是我实际上不知道该怎么办^^。 My problems about this are: 我的问题是:
"{"
and "%"
) 如何处理特殊字符(例如"{"
和"%"
) "'"
and "'"
如何提取"'"
和"'"
之间的字符串 Update 更新资料
What about if I have a whole text which has multiple appearances of an include? 如果我有一个包含多个外观的全文,该怎么办?
<form>{% include '../form.html' %}</form>
<table>{% include '../table.html' %}</table>
There is nothing special to do: 没有什么特别的事情要做:
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);
Try this: 尝试这个:
\{% include '(.+?)' %\}
http://regex101.com/r/nY1aP2 http://regex101.com/r/nY1aP2
By using the non-greedy .+?
通过使用非贪婪的.+?
and capturing it out, your first capture group will include the ./form.html
that you want. 并捕获它,您的第一个捕获组将包含所需的./form.html
。
You can use: 您可以使用:
s="<form>{% include '../form.html' %}</form>";
re = /\{% +include +(['"])([^'"]+)\1/;
fn = s.match(re)[2]; //=> ../form.html
you can do it like: 您可以这样做:
var text = "<form>{% include '../form.html' %}</form>";
var extractedUrl = /(?:include '(.*?)' %)/.exec(text)[1];
You could just follow the input pattern, the whole match is in group 0 您可以按照输入模式进行操作,整个匹配项在第0组中
the part between quotes is group 1. 引号之间的部分是第1组。
# /<form>{%[ \t]+include[ \t]+'([^']+)'[ \t]+%}<\/form>/
<form>{% [ \t]+ include [ \t]+
'
( [^']+ ) # (1)
'
[ \t]+ %}</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.