I am new to Regexes. Now I need to write one to suit my needs. I have this string:
1 [00:00:12.00 – 00:01:20.00] Hello there – I’ve come to help you.
I would somehow need to bring it to this form:
JavaScript
x
6
1
const extracted = [
2
"1",
3
"[00:00:12.00 - 00:01:20.00]",
4
"Hello there - I've come to help you."
5
]
6
I have tried with this approach:
JavaScript
1
3
1
const testSubject = "1 [00:00:12.00 - 00:01:20.00] Hello there - I've come to help you."
2
let result = testSubject.match(/$[^$]++$/)
3
But I am getting this error:
Invalid regular expression: /$[^$]++$/: Nothing to repeat
I have used this place to get the pattern: http://regex.inginf.units.it/
Advertisement
Answer
As already pointed out by anubhava, possessive quantifiers ++
are not supported in Javascript. You can see the error message in this demo when selecting Javascript at the left panel.
There is no $
in the string, but if you want to use a negated character class not matching the brackets, you might use a negated character class with a capture group and use split.
JavaScript
1
3
1
const pattern = /s*([[^][]+])s*/;
2
const s = "1 [00:00:12.00 - 00:01:20.00] Hello there - I've come to help you.";
3
console.log(s.split(pattern))