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

Are there differences between these two?replace([^a-z0-9]gi, '');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

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+017F LATIN SMALL LETTER LONG S and K U+212A KELVIN SIGN under simple casefolding, and potentially if you had a + perhaps also multicharacter folds like ß U+00DF LATIN SMALL LETTER SHARP S and FB00 LATIN 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 need XRegExp for that. – tchrist Commented Sep 4, 2011 at 5:41
 |  Show 1 more ment

1 Answer 1

Reset to default 6

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1741313029a4340047.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信