Reputation: 23
I have text formatted like this:
(delimiter)
111111
(delimiter)
222222
(delimiter)
333333
(delimiter)
Where (delimiter) is a character group and 111111,222222,333333 - some random text, which does not include (delimiter) group.
Is there any way to write a regexp like "(delimiter) <anything,but a delimiter> (delimiter)"?
I expect capturing groups like this:
"(delimiter)
111111
(delimiter)",
"(delimiter)
222222
(delimiter)",
"(delimiter)
333333
(delimiter)"
More specifically,
In my case delimiter is ([0-9]{1,2}[\s][A-z]+[\s][0-9]{4}[\s])
I tried to use negative lookahead like this:
(([0-9]{1,2}[\s][A-z]+[\s][0-9]{4}[\s])(?!([0-9]{1,2}[\s][A-z]+[\s][0-9]{4}[\s]))([0-9]{1,2}[\s][A-z]+[\s][0-9]{4}[\s]))
But that doesn't seem to work for me.
Upvotes: 2
Views: 216
Reputation: 780949
You can use a regular expression as the delimiter in the string split()
method. It will return all the values between the delimiters.
let string = `12 Dec 2019
111111
13 Dec 2019
222222
14 Dec 2019
333333
15 Dec 2019
`;
let result = string.split(/\s*[0-9]{1,2}\s[A-Z]+\s[0-9]{4}\s/i);
console.log(result);
I changed [A-z]
to [A-Z]
and add the i
modifier to make it case-insensitive. There's also no need for []
around \s
.
Upvotes: 1