Javascript 正则表达式中的 \d 有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4541133/
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 14:02:44  来源:igfitidea点击:

What does \d in Javascript regex do?

javascriptregex

提问by DarkLightA

What does \ddo in this example?

\d在这个例子中做什么?

/Chapter (\d+)\.\d*/

回答by alexn

In what context?

在什么情况下?

In a regular expression, it matches a digit (0-9).

在正则表达式中,它匹配一个数字 (0-9)。

Edit, according to your comment:

编辑,根据您的评论:

It matches any string starting with Chapter, followed by digits, then a dot, then a number of digits. Like Chapter 1.0and Chapter 12.01.

它匹配任何以 Chapter 开头的字符串,后跟数字,然后是一个点,然后是一些数字。喜欢Chapter 1.0Chapter 12.01

回答by Mark Baijens

In a regex it means digits 0-9

在正则表达式中,它表示数字 0-9

\dDigits 0 through 9 /H\d/matches "H3"

\d数字 0 到 9 /H\d/匹配“H3”

回答by Bryan Field

If it is in a regular expression (matchor replaceor split) or a /.../string then it probably means match any digit 0-9. Please provide the code you see it in so we can be sure.

如果它在正则表达式(matchorreplacesplit)或/.../字符串中,那么它可能意味着匹配任何数字 0-9。请提供您看到的代码,以便我们确定。