From the following string:
Some random text before pattern e-1-e-20-e-3
I'd like to extract "Some random text before pattern" and [1, 20, 3].
I thought it'd be simple and tried a few different things but none of them have been working so far.
Here's my last try:
(() => {
const text = 'Some random text --- e-1-e-20-e-3';
const re = /(.*)(?:\-?e\-([0-9]{1,2})\-?)+/g;
const matches = [];
let match = re.exec(text);
while (match != null) {
matches.push(match[1]);
match = re.exec(text);
}
console.log(matches)
})()
The previous returns ["3"] and I do not understand why. I've read: - Getting all subgroups with a regex match - Javascript - Regex access multiple occurrences
How do I solve this problem?
EDIT:
I've changed
I'd like to extract [1, 20, 3].
To
I'd like to extract "Some random text before pattern" and [1, 20, 3].
I guess my question is, can I do that with only one regex or do I have to split my search in two?