user4785882
user4785882

Reputation: 91

Regex match 4 to 60 characters, space, single quote and exclude trailing spaces

I need to match 4 to 60 characters, space, single quote, and exclude trailing spaces.

Cases:

  1. "aa aa" - match
  2. "tes'" - match
  3. "not " - not match
  4. "asdpijfaousdhfaoijsdgohasd' asdfa adsfads" - match

I need to exclude trailing spaces from this regex

^[\\w\'\ ]{4,60}$

[ \t]+$ - don't know how to add this to this regex

Upvotes: 1

Views: 96

Answers (1)

The fourth bird
The fourth bird

Reputation: 163477

You might use a pattern where the repeating parts starts with either a space or tab. At the start of the pattern, you could assert a length of 4 - 60 chars using a positive lookahead.

^(?=[\w' \t]{4,60}$)[\w']+(?:[ \t][\w']+)*$

Regex demo

const pattern = /^(?=[\w' \t]{4,60}$)[\w']+(?:[ \t][\w']+)*$/;
[
  "aa aa",
  "tes'",
  "not ",
  "asdpijfaousdhfaoijsdgohasd' asdfa adsfads"
].forEach(s =>
  console.log(`${s} --> ${pattern.test(s)}`)
)

Upvotes: 3

Related Questions