最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论