Sumanth Dosapati
Sumanth Dosapati

Reputation: 15

regular expression to check if every comma is seperated by a space

I need to write a regular expression to find if every comma is followed by a space or not. If atleast one comma is not followed by a space then it should return true. If all the commas present are definitely followed by space then return false.

Example :hi, hello are two words. one, two are numbers this should return false.
hi, hello are two words. one,two are numbers this should return true because there is no space after one comma

Upvotes: 0

Views: 819

Answers (2)

thinkgruen
thinkgruen

Reputation: 1

You can see if this regex produces any matches: ,(?! ) - In this case it means there is a comma that is not followed by a space. If one exists, then you should return true.

Upvotes: 0

Joel Imbergamo
Joel Imbergamo

Reputation: 369

I think this should work:

/,[^ ]/g

Explanation: get all the commas that do not have a space just after it.

For it to return a boolean you need to use the test method for regex in js: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test

Upvotes: 2

Related Questions