I am taking an ISBN input by the user - may contain spaces and hyphens etc. - and trying to sanitise it to be only digits.
In Java and Javascript, I have used the following regex successfully
Java (isbn is a java.lang.String)
isbn = isbn.replaceAll("[^\\d]", "");
and, JavaScript
isbn = isbn.replace(/[^\d]/g, "");
However, some ISBNs can have an X as their checksum character. For example, 'The book of days' by Sara Reinke is '155404295X'
How can I change the regex to allow X as well as digits?
Update: [^\dX]
worked in JavaScript, but [^\\dX]
does not work in Java.
Update 2: PEBKAC! I was sanitising in two places - I updated one but not the other. [^\\dX]
does work in Java as well.
Can you try [^0-9X]
there? I think it will work in both Java and JavaScript.
P.S. But \\d
should work in Java too...