请解释正则表达式/[\d\.]+/g中的[]、+、.语法含义
Hey there! Let's break down each part of that regex you're confused about—super glad you already grasp the global matching (g flag) logic, so we can focus straight on the syntax bits that are tripping you up.
/[\d\.]+/g 中的关键语法元素 Let's go one by one:
[](字符集/Character Set)
Think of this as a "menu" of characters the regex can pick from. Any single character inside the brackets is a valid match. In your case,[\d\.]means "match either a digit or a literal dot". A quick note: most special regex characters lose their special meaning inside brackets, but we still escape the dot with\here to make it explicitly clear we're targeting the literal.(not the wildcard dot we'll talk about next).+(量词/Quantifier)
This tells the regex: "match the previous element (in this case, the[\d\.]character set) one or more times in a row". Without this, your regex would match each digit individually (so55+60would return["5", "5", "6", "0"]). With the+, it grabs consecutive digits (or digits plus dots) as a single match—hence["55", "60"]from your example.\.(转义的点号/Escaped Dot)
Normally, an unescaped.in regex is a wildcard that matches any single character except a newline. But when we add the backslash (\), we "escape" it, turning it into a literal dot character. This lets the regex match decimal numbers like"3.14"or"100.5"if they show up in your string. If you only ever work with integers, you could omit this, but it's a nice addition for flexibility.
To make it concrete: if your string was "55.2+60", num.match(/[\d\.]+/g) would return ["55.2", "60"]—you can see how the \. and + work together to capture the full decimal number.
内容的提问来源于stack exchange,提问作者Kabil

