BBQ
BBQ

Reputation: 618

regex to filter sentences with minimum word lengths

I cant seem to find a regular expression to filter sentences with word lengths less than 'n' characters.

Example: 'n' = 3

"Hello World, Hello Again" is no match

"Hello World, Javascript is Crazy" is a match because 'is' has less than 3 characters.

Upvotes: 0

Views: 725

Answers (2)

kojiro
kojiro

Reputation: 77089

[11:42:43.562] /\b\w{1,2}\b/.test("Hello World, Hello Again")
[11:42:43.565] false
--
[11:43:09.002] /\b\w{1,2}\b/.test("Hello World, Javascript is Crazy")
[11:43:09.005] true

Use {1,2} if you want less than, use {1,3} if you want less than or equal.

Upvotes: 4

Mike Brant
Mike Brant

Reputation: 71384

Try this pattern:

var pattern = '/\b.{1,3}\b/';

Upvotes: 0

Related Questions