To improve the current expression, consider moving the opening (
right after the :
, like this: /\(CATCH:(.*?)\)/
. Then, to extract Group 1 value, you can use something similar to
var titleData = string.match(TitleRegex)[1]
.
A more precise pattern suggestion:
var string = '(CATCH: dummy)';
var TitleRegex = /\(CATCH:\s*([^()]*)\)/;
var titleData = string.match(TitleRegex);
if (titleData) {
console.log(titleData[1]);
}
The regex pattern is \(CATCH:\s*([^()]*)\)
:
\(CATCH:
- matches (CATCH:
\s*
- zero or more white spaces
([^()]*)
- Capturing group 1: any characters except (
and )
\)
- matches )
You can opt for /\(CATCH:([^()]*)\)/
(without \s*
) and then use titleData[1].trim()
to remove any leading or trailing whitespaces from the extracted value.