string = string.replace(/[^a-zA-Z0-9]/g, '');
Can anyone Explain this snippet (/[^a-zA-Z0-9]/g, '') ??? What it means? I know what it does. But how
The snippet
string = string.replace(/[^a-zA-Z0-9]/g, '');
Uses RegEx (Regular Expression) to find a paten and then replace it.
This is how it works:
/[^a-zA-Z0-9]/g
is the regex. a-z
means lower case characters from a-z, A-Z
means upper case characters from a-z and 0-9
means number characters from 0-9. The ^
means not, so, not a letter, uppercase letter or number. The g
at the end stands for global meaning it will not just find one match for the patern but all the matchesRead about RegEx here