1

I know that the regex class \D matches "all characters that are non-numeric" but I would like to match on all characters that are non-numeric and are not / or -

How might I do this? Thanks!

1
  • Be it for JavaScript or any other language, ndn's answer is "the best". In Java, you could use [\\D&&[^/-]], in .NET [\D-[/-]]. Commented Jan 12, 2016 at 21:37

2 Answers 2

3

You can negate character sets by putting ^ inside:

[^\d\/-]

Will match any one character, which is not a digit, forward slash or dash.

Sign up to request clarification or add additional context in comments.

Comments

0

You already know how to find non-numeric characters with \D. You may lay a restriction on the \D to exclude / and - and any other non-numeric characters with a negative lookahead:

(?![\/-])\D

See regex proof.

EXPLANATION

--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    [\/-]                    any character of: '\/', '-'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  \D                       non-digits (all but 0-9)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.