Reputation: 13412
Was playing around with regexp to remove all numbers from string
I came up to this:
/([^0-9])$/
But it works only if string looks like this, e.g. Name123 but if you enter Name123Name than it doesn't work?
Can't understand why?
Any ideas?
Best regards, Ilia
Upvotes: 0
Views: 77
Reputation: 838416
Your regular expression finds one character not in [0-9]
at the end of the string.
To check if there is a digit anywhere in the string, remove the anchors:
/[0-9]/
To check that all characters are not digits, add a start of string anchor too:
/^[^0-9]+$/
This approach is called a blacklist - a list of characters you don't want to allow. Note that it's often better to create a whitelist instead - a list characters that you do want to allow.
Upvotes: 4
Reputation: 1615
remove the $
at the end, because that matches the end of the string. Also you can use \d
to match a digit instead of [0-9]
depending on the language you're using.
so in your example /[0-9]$/
matches Name123 because the 123 appears at the end of the string, thus matches the $
anchor. But in the other example, Name123Name, the $
anchor doesn't match because the digits are in the middle of the string.
Upvotes: 1