Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regular Expression For Allowing Spaces Between Words For JavaScript

I want a regular expression that allows spaces between specific words. The regex below works great, but it doesn’t allow for spaces between words.

/\b(ADD|ADD CONSTRAINT|ALL|ALTER)(?=[^\w])/g

For example, when using this regular expression (ADD CONSTRAINT), only ADD is matched, the remaining CONSTRAINT does not matches.

It should match the both ADD CONSTRAINT including the space.
But don’t recommend to add CONSTRAINT separately, because it will match it separately and I don’t want this mess.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How can I tweak it to allow spaces?

Regex Example: https://regex101.com/r/FKphXU/1

>Solution :

You could add the second word as optional to your regex:

/\b(ADD(?: CONSTRAINT)?|ALL|ALTER)(?=[^\w])/g
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading