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
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./myregexp-test
Share Improve this question edited Sep 4, 2011 at 5:18 ajax333221 asked Sep 4, 2011 at 4:20 ajax333221ajax333221 11.8k16 gold badges62 silver badges95 bronze badges 6-
4
Be aware of what it actually does. Try
"naïve".replace(/[^a-z0-9]/gi, '');
– NullUserException Commented Sep 4, 2011 at 4:43 -
@NullUserException but in the end there isn't a solution in JS to this problem. Even
\W
does the same. – xanatos Commented Sep 4, 2011 at 5:12 -
2
@xanatos: That is not strictly true. The XRegExp plugin for Javascript gives access to the Unicode general category, script, and block properties that Javascript on its own so scandalously neglects. That means that with it you can use the
\pL
and\pN
and such, as well as\p{Latin}
,\p{Common}
, etc. – tchrist Commented Sep 4, 2011 at 5:17 - +1 I didn't know there was a JS replacement for Regex. Very good to know. – xanatos Commented Sep 4, 2011 at 5:20
-
@ogps92: With Unicode casefolding,
/[A-Z]/i
can pick up things that/[a-zA-Z]/
misses. For example,ſ
U+017FLATIN SMALL LETTER LONG S
andK
U+212AKELVIN SIGN
under simple casefolding, and potentially if you had a+
perhaps also multicharacter folds likeß
U+00DFLATIN SMALL LETTER SHARP S
andff
FB00LATIN SMALL LIGATURE FF
. However, Javascript doesn’t support Unicode case-insensitive matches, but nearly everything else does, so don’t get too placent. In general\pL
is how to pick up all letters and\p{Lu}
the uppercase ones, but you needXRegExp
for that. – tchrist Commented Sep 4, 2011 at 5:41
1 Answer
Reset to default 6Nope, 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.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1741313029a4340047.html
评论列表(0条)