For example, I have the string 11aa32bbb5
and I want to extract only the characters aa
, bbb
or the numbers11, 32, 5
How do I confirm that a letter or string is alphanumeric or not?
This is the implementation I have.
const checkChar = (ch) => {
if (ch.charCodeAt(0) >= 97 && ch.charCodeAt(0) <= 122) {
return true;
};
return false;
};
Thank you!
You can use a regular expression, as follows
We replace both from A to Z and all the numbers of the string, so that if it is empty, it will logically be alphanumeric
We combine 3 regular expressions:
So we count from 0 to 9 for each number in the string, in addition to the letters A to Z in upper and lower case, we will use the /g operator to indicate that we are going to replace all existing matches, if we wanted to extract only the first matches would be as follows
So the /i operator indicates ignore case , that is, it is not case sensitive.
To extract the matches we can use
.match