简体   繁体   English

正则表达式匹配日期中的数字但不应该

[英]Regex matches numbers in date but shouldn't

Why does my regex pattern match the date part of the string?为什么我的正则表达式模式与字符串的日期部分匹配? It seems like I'm not accounting for the / (slash) correctly with [^\\/] to avoid the pattern to match date strings?似乎我没有用[^\\/]正确地考虑/ (斜线)以避免匹配日期字符串的模式?

 const reg = new RegExp( /(USD|\\$|EUR|€|USDC|USDT)?\\s?(\\d+[^\\/]|\\d{1,3}(,\\d{3})*)(\\.\\d+)?(k|K|m|M)?\\b/, "i" ); const str = "02/22/2021 $50k"; console.log(reg.exec(str)); // result: ['02', undefined, '02', undefined, undefined, undefined, undefined, index: 0, input: '02/22/2021 $50k', groups: undefined] // was expecting: [$50k,...]

You get those matches for the date part and the undefined ones, because you use a pattern with optional parts and alternations |您将获得日期部分和未定义部分的匹配项,因为您使用了带有可选部分和交替的模式|

In your pattern there is this part (\\d+[^\\/]|\\d{1,3}(,\\d{3})*) .在你的模式中有这部分(\\d+[^\\/]|\\d{1,3}(,\\d{3})*) That first part of the alternation \\d+[^\\/] matches 1+ digits followed by any char except a / (which can also match a digit) and the minimum amount of characters is 2. That part will match 20, 22 and 2021 in the date part.交替\\d+[^\\/]第一部分匹配 1+ 个数字,后跟除/ (也可以匹配数字)之外的任何字符,并且最小字符数为 2。该部分将匹配 20、22 和 2021在日期部分。

If there is 1 digit, the second part of the alternation will match it.如果有 1 个数字,则交替的第二部分将匹配它。


If you want to match only numbers as well, you can assert not / to the left and the right, and make the whole part with the first alternatives like USD optional with the optional whitspace chars as well, to prevent matching that before only digits.如果你只想匹配数字,你可以断言 not /到左边和右边,并使整个部分与第一个替代品如 USD 可选,以及可选的 whitspace 字符,以防止仅在数字之前匹配。

The last alternation can be shortened to a character class [km]?最后一个交替可以缩短为一个字符类[km]? with a case insensitive flag.带有不区分大小写的标志。

See this page for the lookbehind support for Javascript.请参阅此页面以了解对 Javascript 的后视支持

(?:(?:USD|\$|EUR|€|USDC|USDT)\s?)?(?<!\/)\b(?:\d{1,3}(?:,\d{3})*(?:\.\d+)?|\d+)(?!\/)[KkMm]?\b

Regex demo正则表达式演示

 const reg = /(?:(?:USD|\\$|EUR|€|USDC|USDT)\\s?)?(?<!\\/)\\b(?:\\d{1,3}(?:,\\d{3})*(?:\\.\\d+)?|\\d+)(?!\\/)[KkMm]?\\b/gi; const str = "02/22/2021 $50k 1,213.3 11111111 $50,000 $50000" const res = Array.from(str.matchAll(reg), m => m[0]); console.log(res)

If the currency is not optional:如果货币不是可选的:

(?:USD|\$|EUR|€|USDC|USDT)\s?(?:\d{1,3}(?:,\d{3})*(?:\.\d+)?|\d+)[KkMm]?\b

Regex demo正则表达式演示

I can't get your regex well.我不能很好地理解你的正则表达式。 so i try to figure out what result you would expect.所以我试着弄清楚你会期望什么结果。 check this.检查这个。 in groups you have each part of your string.在组中,您拥有字符串的每个部分。

 const regex = /(\\d{2})*\\/?(\\d{2})\\/(\\d{2,4})?\\s*(USD|\\$|EUR|€|USDC|USDT)?(\\d*)(k|K|m|M)?\\b/i const regexNamed = /(?<day>\\d{2})*\\/?(?<month>\\d{2})\\/(?<year>\\d{2,4})?\\s*(?<currency>USD|\\$|EUR|€|USDC|USDT)?(?<value>\\d*)(?<unit>k|K|m|M)?\\b/i const str1 = '02/22/2021 $50k' const str2 = '02/2021 €50m' const m1 = str1.match(regex) const m2 = str2.match(regexNamed) console.log(m1) console.log(m2.groups)

Blockquote块引用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM