Javascript 这是什么代码?/^(\d{4}|\d{6})$/
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34389059/
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
What code is this? /^(\d{4}|\d{6})$/
提问by Tucker Beauchamp
So I am extremely new to the Javascript world. I was practicing on codewars having to analyze a pin to make sure it only contained numbers and was either 4 or 6 characters. I looked at the most clever code and the answer was:
所以我对 Javascript 世界非常陌生。我正在练习代码战,必须分析一个 pin 以确保它只包含数字并且是 4 或 6 个字符。我看了最聪明的代码,答案是:
function validatePIN(pin) {
return /^(\d{4}|\d{6})$/.test(pin)
}
I've never seen the "/^(\d{4}|\d{6})$/" bit before. Could anyone tell me what this is called so I can research it on my own, or give me a breakdown of how it works?
我以前从未见过 "/^(\d{4}|\d{6})$/" 位。谁能告诉我这叫什么,这样我就可以自己研究它,或者给我详细说明它是如何工作的?
回答by Draco18s no longer trusts SE
It's a regular expression.
这是一个正则表达式。
I tend to use http://www.regexpal.com/when I want to try and find the expression I need, there's also http://regexr.com/for learning about them (among other resources).
当我想尝试找到我需要的表达式时,我倾向于使用http://www.regexpal.com/,还有http://regexr.com/用于了解它们(以及其他资源)。
回答by STW
It's a regular expression literal, similar to using return new RegExp('^(\\d{4}|\\d{6})$').test(pin)
The "literal" part implies that it's a means of representing a specific data type as a string in code—just like true
and 'true'
are different, as one is a boolean literal and the other is a string literal.
它是一个正则表达式字面量,类似于使用return new RegExp('^(\\d{4}|\\d{6})$').test(pin)
“字面量”部分意味着它是一种将特定数据类型表示为代码中的字符串的方法——就像true
和'true'
不同,一个是布尔字面量,另一个是字符串字面量.
Specifically, the regex ^(\d{4}|\d{6})$
breaks down to:
具体来说,正则表达式^(\d{4}|\d{6})$
分解为:
^ a string that starts with...
( either
\d a digit (0-9)...
{4} that repeats four times...
| or
\d a digit (0-9)...
{6} that repeats six times...
)
$ and then ends
So: '1234'
, '123456'
, etc would match. '123.00'
, '12345'
,'abc123'
,' 1234'
, ' 1234 '
would not match.
所以: '1234'
, '123456'
, 等会匹配。'123.00'
, '12345'
, 'abc123'
, ' 1234'
,' 1234 '
不匹配。
As noted by several others in the comments on Draco18s' answerthere are several nuances to be aware of with using regex literals in JS:
正如其他几个人在对Draco18s 回答的评论中所指出的,在 JS 中使用正则表达式文字时需要注意几个细微差别:
The literal syntax doesn't require you to escape special characters within the regex pattern. Using the RegExp constructor requires you to represent the pattern as a string, which in turn requires escaping. Note the differences of the
\
's between the two syntaxes.Using a regex literal will treat the regex as a constant, whereas using
new RegExp()
leaves life cycle management of the regex instance up to you.The literal notation is compiled and implies a constant regex, whereas the constructor version is reparsed from the string, and so the literal is better optimized/cached. jsperf.com/regexp-literal-vs-constructor/4Note: you can get basically the same effect by caching the new Regex in a variable, but the literal one is cached at the JIT step – user120242
In other words, using a regex literal can avoid potential performance pitfalls:
Example:
for (var i = 0; i < 1000; i++) { // Instantiates 1x Regex per iteration var constructed = new RegExp('^(\d{4}|\d{6})$') // Instantiates 1 Regex var literal = /^(\d{4}|\d{6})$/ }
文字语法不需要您在正则表达式模式中转义特殊字符。使用 RegExp 构造函数要求您将模式表示为字符串,而这又需要转义。请注意
\
's 在两种语法之间的差异。使用正则表达式文字会将正则表达式视为常量,而使用
new RegExp()
正则表达式实例的生命周期管理由您决定。文字符号被编译并暗示一个常量正则表达式,而构造函数版本是从字符串中重新解析的,因此文字被更好地优化/缓存。jsperf.com/regexp-literal-vs-constructor/4注意:通过将新的 Regex 缓存在一个变量中可以获得基本相同的效果,但字面值是在 JIT 步骤缓存的 – user120242
换句话说,使用正则表达式可以避免潜在的性能缺陷:
例子:
for (var i = 0; i < 1000; i++) { // Instantiates 1x Regex per iteration var constructed = new RegExp('^(\d{4}|\d{6})$') // Instantiates 1 Regex var literal = /^(\d{4}|\d{6})$/ }
回答by user120242
Good reference for Javascript RegExp
http://www.regular-expressions.info/javascript.html
Javascript RegExp 的良好参考
http://www.regular-expressions.info/javascript.html
^ beginning of line
\d = all digits
{4} = repetition 4 times
| = "or"
$ end of line
your example tests for a 4 digit string or 6 digit string
您的示例测试 4 位字符串或 6 位字符串