Reputation: 136
I want to transfer the results between Regex and two words to an array, but unfortunately I couldn't this. Can you help me?
In this text
[row]
Row text1
[rowEnd]
[row]
Row text2
[rowEnd]
I will search this content,
[row]
(.*)
[rowEnd]
Based on this I write a regex like this
/(\[row\]+)(.*)(\[rowEnd\])/gs
However, this way, it takes the whole, not piece by piece.
Thank you in advance for your help.
Upvotes: 0
Views: 572
Reputation: 8168
"[row]"
."[rowEnd]"
."[rowEnd]"
from the filtered strings.const
data = "[row]Row text1[rowEnd][row]Row text2[rowEnd]",
res = data
.split("[row]")
.filter((s) => s.endsWith("[rowEnd]"))
.map((s) => s.slice(0, -8));
console.log(res);
Upvotes: 1
Reputation: 163217
In Javascript, you could use
^\[row]\r?\n([^]*?)\r?\n\[rowEnd]
^
Start of string\[row]\r?\n
Match [row]
and a newline(
Capture group 1
[^]*?
Match 0+ times any char including a newline non greedy)
Close group 1\r?\n\[rowEnd]
Match a newline and [rowEnd]
const regex = /^\[row]\r?\n([^]*?)\r?\n\[rowEnd]/gm;
const str = `[row]
Row text1
[rowEnd]
[row]
Row text2
[rowEnd]`;
Array.from(str.matchAll(regex), m => console.log(m[1]));
Upvotes: 4