1

I'm using this regexp:

/[^+][a-z]/.test(str)

I'm trying to ensure that if there are any letters ([a-z]) in a string (str) not proceeded by a plus ([^+]) , a match is found and therefore it will return true.

It mostly works except when there is only one character in the string. For example, a returns false, even though there is no plus sign preceding it. How can I ensure it works for all strings including one character strings. Thanks!

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Evelyn Zouras
  • 95
  • 1
  • 10

1 Answers1

1

Add a ^ as an alternative to [^+]:

/(?:^|[^+])[a-z]/.test(str)
 ^^^^^^^^^^

The (?:^|[^+]) is a non-capturing alternation group matching either the start of the string (with ^) or (|) any char other than + (with [^+]).

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    ah - didn't know to do that. thanks. Also the links explaining the alternative you presented was very helpful. thanks again. – Evelyn Zouras Sep 28 '17 at 13:39