wyc
wyc

Reputation: 55273

Regex to capture quotation mark followed by a non-punctuation character

I had the following regex to match opening quotation marks:

/'(?=\b)/g

But then I realized that it also captures stuff like don't and it's.

So I added another rule:

/'(?=\b[^a-zA-Z])/g

Capture an opening quotation mark not followed by a letter (t and s in this case). But now none of the quotation marks are being highlighted.

Did I modify the regex in a wrong way?

EDIT:

Oh, I realized my dumb mistake. Anyway, here's what I want to do:

"two 'three'"

"four don't it's "

I want to match the opening ' in three but not the 's in don't and it's

Upvotes: 0

Views: 254

Answers (4)

cнŝdk
cнŝdk

Reputation: 32145

Referring to your last EDIT, this Regex /[\.\,\;\?\!\s]+'/g is what you are looking for, it matches quotation in sentences like 'three' and skips what you need (sentences like don't and it's).

Here's a DEMO.

Upvotes: 1

Jordan Honeycutt
Jordan Honeycutt

Reputation: 338

You have your look-around in the wrong place. You want to find instances where a quote is preceded by nothing, and proceeded by characters. This would probably work

/\b'\B/

Upvotes: 2

halex
halex

Reputation: 16403

You can use the following regex to only match opening '

/(?!\b)'/g

See the demo on your testcases

Upvotes: 2

anubhava
anubhava

Reputation: 785098

You can try this regex to match quotes but skip those cases like don't it's etc:

/(?:^|[^a-zA-Z])'|'(?![a-zA-Z])/gm

RegEx Demo

Upvotes: 2

Related Questions