Are there differences between these two?
replace(/[^a-z0-9]/gi, '');
replace(/[^a-zA-Z0-9]/g, '');
Also, are there any significant differences in time using one or another?
edit: about the performance, I did some testing http://jsperf.com/myregexp-test
It's regular expression. "[aeiou]" tells the replace function to look for any of these characters and "gi" are flags that tells the function to look for match over the entire string (will otherwise break at the first match), this is the "g" flag.
The bracketed characters [a-zA-Z0-9] mean that any letter (regardless of case) or digit will match. The * (asterisk) following the brackets indicates that the bracketed characters occur 0 or more times.
In a regular expression, if you have [a-z] then it matches any lowercase letter. [0-9] matches any digit. So if you have [a-z0-9], then it matches any lowercase letter or digit.
Using character sets For example, the regular expression "[ A-Za-z] " specifies to match any single uppercase or lowercase letter. In the character set, a hyphen indicates a range of characters, for example [A-Z] will match any one capital letter. In a character set a ^ character negates the following characters.
Nope, by the first, the i
at the end makes the regex case insensitive meaning that it doesn't matter if the letter it finds is upper- or lower-case.
The second matches upper- and lower-case letters but makes sure they are either upper- or lower-case. So you end up with the same result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With