Reputation: 495
how can i except \d in below regex?
^[\d\w ]+$
it Matches strings with any words and digits and space characters. i want to change this to accept strings with any words and space characters.
i try below regex but it excepts digits and space too.
^[^\d\w ]+$
Upvotes: 1
Views: 807
Reputation: 627082
You may use
^(?:[^\W\d]| )+$
See the regex demo. It can also be written as ^(?:(?!\d)[\w ])+$
.
Details
^
- start of string(?:[^\W\d]| )+
- 1 or more
[^\W\d]
- chars other than non-word and digit chars|
- or
- whitespace$
- end of string.In ^(?:(?!\d)[\w ])+$
, [\w ]
that may match any word and whitespace char, is restricted with (?!\d)
lookahead and thus does not match digits.
Or, if you need ASCII only words:
^[a-zA-Z_ ]+$
Please bear in mind it will also allow underscores since \w
matches _
chars. If you do not need it, use ^(?:[^\W\d_]| )+$
/ ^(?:(?![\d_])[\w ])+$
/ ^[a-zA-Z ]+$
.
Upvotes: 1