正则表达式位于rubular页面中且完美运行:http://rubular.com/r/vtflEgDdkY
但在本地主持人中,我无法使用它...... 这是我的代码
var file = readTextFile("story.txt") // not empty
var result = file.match(/(::head.*?$^.*?$)^\s*$/m)
console.log(result) // writes null
file 变量不为空且readTextFile()函数正在运行。
这是story.txt
::head
line 1
line 2
line 3
::content
content 1
content 2
content 3
正则表达式无法正常工作......问题出在哪里?
答案 0 :(得分:0)
如果您希望同时捕获头部和内容,可以使用此表达式
::head\n((?:(?!\n+::).)*)\n+::content\n((?:(?!\n+::).)*)$
现场演示
https://regex101.com/r/oX8vR6/1
示例文字
::head
line 1
line 2
line 3
::content
content 1
content 2
content 3
样本匹配
head
content
1. [7-27] `line 1
line 2
line 3`
2. [39-69] `content 1
content 2
content 3 `
Javascript代码示例:
<script type="text/javascript">
var re = /::head[\r\n]((?:(?![\r\n]+::).)*)[\n\r]+::content[\r\n]((?:(?![\r\n]+::).)*)$/;
var sourcestring = " ---- your sample text goes here ---- ";
var results = [];
var i = 0;
for (var matches = re.exec(sourcestring); matches != null; matches = re.exec(sourcestring)) {
results[i] = matches;
for (var j=0; j<matches.length; j++) {
alert("results["+i+"]["+j+"] = " + results[i][j]);
}
i++;
}
</script>
示例输出
$matches Array:
(
[0] => Array
(
[0] => ::head
line 1
line 2
line 3
::content
content 1
content 2
content 3
)
[1] => Array
(
[0] =>
line 1
line 2
line 3
)
[2] => Array
(
[0] =>
content 1
content 2
content 3
)
)
NODE EXPLANATION
----------------------------------------------------------------------
::head '::head'
----------------------------------------------------------------------
\n '\n' (newline)
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
----------------------------------------------------------------------
(?! look ahead to see if there is not:
----------------------------------------------------------------------
\n+ '\n' (newline) (1 or more times
(matching the most amount possible))
----------------------------------------------------------------------
:: '::'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
. any character except \n
----------------------------------------------------------------------
)* end of grouping
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
\n+ '\n' (newline) (1 or more times (matching
the most amount possible))
----------------------------------------------------------------------
::content '::content'
----------------------------------------------------------------------
\n '\n' (newline)
----------------------------------------------------------------------
( group and capture to \2:
----------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
----------------------------------------------------------------------
(?! look ahead to see if there is not:
----------------------------------------------------------------------
\n+ '\n' (newline) (1 or more times
(matching the most amount possible))
----------------------------------------------------------------------
:: '::'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
. any character except \n
----------------------------------------------------------------------
)* end of grouping
----------------------------------------------------------------------
) end of \2
----------------------------------------------------------------------
$ before an optional \n, and the end of a
"line"
----------------------------------------------------------------------
答案 1 :(得分:0)
怎么样:
var s = "::head\nline 1\nline 2\nline 3\n\n::content\ncontent 1\ncontent 2\ncontent 3\n\n";
var r = s.match(/(::head[\s\S]+?)\n\n/);
console.log(r);
<强>输出:强>
["::head\nline 1\nline 2\nline 3\n\n", "::head\nline 1\nline 2\nline 3"]