Reputation: 103
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!
Upvotes: 1
Views: 45
Reputation: 627517
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 [^+]
).
Upvotes: 1