Suppose I have a text like this:
This is the title: \ titles always have a colon
This is a regular sentence.
A sentence always ends with a period.
A sentence can
span multiple lines.
A sentence can contain numbers like 123.
The phrase can also contain "text enclosed in double quotes" or 'text enclosed in single quotes'.
Other symbols that may appear in sentences are
the comma ,
the semicolon ;
the dollar sign $
parentheses ( )
the plus sign the minus sign - and the square brackets[ ].This is an isolated phrase that the regular expression should not match.
How could I create a regex in javascript to match the text between the first colon* and the last full stop after brackets[ ]
? (assuming there will be no other colon, if there are any, they will be enclosed in double quotes)
I've tried using :.*
but it maches all lines.
CodePudding user response:
Using the s flag, the following regex captures everything after the colon till the double linebreak.
/(?<=:).*?(?=(?:\r?\n) [^\n]*$)/sg
Test on regex101 here