0

I want to write a regex to find whenever there isn't a space after a period or comma, unless there are three periods in a row, in which case it's find to have another word character after.

E.g.

"Hello.how are you" -> Match

"Sounds good..." -> No Match

"...are you sure?" -> No Match

This is what I have so far (quotation marks after ., are fine too), but it finds a match for the third example, which I don't want.

/[.,][^\s"”.]/g 

How can I specify that \.\.\.\w should not match, but any other \.\w should?

(Using js)

3
  • 2
    If using V8+ use lookbehind (?<!\.)[,.][^\s"”.] Commented Mar 25, 2021 at 23:48
  • 1
    Are you replacing? What is the expected output for the Hello.how are you string? If you replace with space, use replace(/(\.{3}\w)|[.,](?![\s"”.])/g, function(x,y) { return y ? y : " "; }) Commented Mar 26, 2021 at 0:07
  • Thanks @ctwheels, that seemed to do the trick! Commented Mar 26, 2021 at 0:28

1 Answer 1

2

I think the following regex does what you need:

(?<!\.\.)[.,](?=[^\s.]) 

It matches any period that isn't preceeded by two periods and that isn't followed by a period or a whitespace.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! Why do I need the positive lookahead?
I guess it depends on what you want to match. In this case I'm only matching the offending period or comma.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.