user1070111
user1070111

Reputation: 59

spicy angularjs regex

I've been working on a regex problem for angularJs ng-pattern which needs:

  1. Cannot be blanks
  2. A minimum of 1 character and a maximum of 32 characters
  3. Spaces ONLY are not allowed
  4. Acceptable special characters(!@#$%&*-+=[]:;',.? )
  5. The answer is not case sensitive
  6. Combination of &# is not allowed
  7. Spaces at the beginning and the end of the answer should be trimmed.

This is my solution which covers all requirement but 6th:

([^a-zA-Z0-9!@#$%& *+=[\]:;',.?-])|(^\s*$)

Do you guys have any ideas?

Upvotes: 1

Views: 46

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626691

You may use

/^(?!\s*$)(?!.*&#)[a-zA-Z0-9!@#$%&*+=[\]:;',.?\s-]{1,32}$/

See the regex demo.

Details

  • ^ - start of string
  • (?!\s*$) - no 0+ whitespaces from start till end of string allowed
  • (?!.*&#) - no &# allowed after any 0+ chars
  • [a-zA-Z0-9!@#$%&*+=[\]:;',.?\s-]{1,32} - 1 to 32 allowed chars: ASCII digits, letters, whitespaces and some punctuation/symbols
  • $ - end of string.

Upvotes: 1

Related Questions