mottosson
mottosson

Reputation: 3763

Regex to include quotes in match between quotes (and new lines)

I'm trying to find strings enclosed in single quotes with this regex : /'+(.*?)'+,?/g

The problem is that single quotes are allowed inside the string as long as they are escaped with a second quote: 'it''s, you''ve, I''m... and so on, ends with one more single quote '''.

My regex breaks if there are any amount of single quotes inside and ends up skipping quotes in the beginning and end of the match if there are any.

It seems to work perfectly as long as nobody adds any quotes inside the string. But this is not how the real world works unfortunately.

How can I make my regex include the quotes in the match?

Upvotes: 1

Views: 183

Answers (1)

Scott Weaver
Scott Weaver

Reputation: 7361

try this regex:

'(?:''|[^'])*'

explanation: single quote followd by (two quotes OR a non quote char) repeated as necessary, followed by a closing single quote.

https://regex101.com/r/R4sd47/1

Upvotes: 1

Related Questions