Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between replace(/[^a-z0-9]/gi, '') and replace(/[^a-zA-Z0-9]/g, '')

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

like image 833
ajax333221 Avatar asked Sep 04 '11 04:09

ajax333221


People also ask

What is GI in replace?

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.

What does a ZA Z0 9 ]+$ mean?

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.

What does a Z0 9 mean?

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.

What does regex AZ mean?

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.


1 Answers

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.

like image 169
qwertymk Avatar answered Sep 22 '22 12:09

qwertymk