adit
adit

Reputation: 33684

javascript regular expression of minimum 3 words and allow special characters

I have the following regex that validates if it's a minimum of 3 words:

 "minimumThreeWordsAndNoURL": {
                    "regex": /^(\b\w+\b\s?){3,}$/,
                    "alertText": "Too short"
                },

Now the issue is that when I do 'How much is this?' it fails because of the special characters. How do I allow special characters in this regex?

Upvotes: 1

Views: 1389

Answers (3)

anubhava
anubhava

Reputation: 786289

The following regex should work:

^(?:(?:^| )\S+ *){3,}$

Online Demo

Upvotes: 1

jojo
jojo

Reputation: 16

try this untested:

(\b.{3,}\b\s)+

Upvotes: 0

sabof
sabof

Reputation: 8192

Does this do what you want?

 /(\b\S+.*?){3,}/.test('How much is this?')

Upvotes: 0

Related Questions