javascript replace(/[^a-z0-9]/gi, '') 和 replace(/[^a-zA-Z0-9]/g, '') 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7297397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 23:38:45  来源:igfitidea点击:

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

javascriptregexreplace

提问by ajax333221

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

编辑:关于性能,我做了一些测试http://jsperf.com/myregexp-test

采纳答案by qwertymk

Nope, by the first, the iat the end makes the regex case insensitive meaning that it doesn't matter if the letter it finds is upper- or lower-case.

不,首先,i最后使正则表达式不区分大小写,这意味着它找到的字母是大写还是小写都没有关系。

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.

第二个匹配大写和小写字母,但确保它们是大写或小写。所以你最终会得到相同的结果。