[英]Overlapping named capturing groups
I'm using named capturing groups to validate and extract data out of a product number. 我正在使用命名的捕获组来验证和提取产品编号中的数据。 The format of the product number looks like this:
产品编号的格式如下:
1102961D048.075
Chars 1-2 gender_code 11
Chars 1-6 style 110296
Chars 7-8 width_code 1D
Chars 9-11 color_code 048
Char 12 delimiter ignored
Chars 13-15 size_code 075
My current code looks like this: 我当前的代码如下所示:
const validateMpn = (mpn) => { const regex = /(?<style>\\d{6})(?<width>\\d{1}[ABDE])(?<color_code>\\d{3})\\.(?<size_code>\\d{3})/gi const match = regex.exec(mpn) if (!match) { return null } return match.groups } const str1 = '1102961D048.075' const str2 = '1200322A001.085' const match1 = validateMpn(str1) const match2 = validateMpn(str2) console.log(match1) console.log(match2)
As gender_code
and style
overlap I'm not sure how to get them both. 由于
gender_code
和style
重叠,因此我不确定如何将两者兼而有之。 Therefore I have the following questions: 因此,我有以下问题:
Sure, just place gender
inside the style
group: 当然,只需将
gender
放入style
组中即可:
const validateMpn = (mpn) => { const regex = /(?<style>(?<gender>\\d{2})\\d{4})(?<width>\\d{1}[ABDE])(?<color_code>\\d{3})\\.(?<size_code>\\d{3})/gi const match = regex.exec(mpn) if (!match) { return null } return match.groups } const str1 = '1102961D048.075' const str2 = '1200322A001.085' const match1 = validateMpn(str1) const match2 = validateMpn(str2) console.log(match1) console.log(match2)
I suggest just having separate capture groups for the first two and four following characters. 我建议对于前两个和后四个字符仅使用单独的捕获组。 Then, form the
style
by just concatenating together the first two capture groups: 然后,通过将前两个捕获组连接在一起来形成
style
:
var input = "1102961D048.075"; var regex = /(.{2})(.{4})(.{2})(.{3}).(.{3})/g; var match = regex.exec(input); console.log("gender_code: " + match[1]); console.log("style: " + match[1] + match[2]);
As a style note, I prefer not using named capture groups, because they tend to result in a bloated regex which is hard to read. 作为样式说明,我更喜欢不使用命名捕获组,因为它们会导致难以理解的膨胀正则表达式。
Yes you can capture gender_code using positive look ahead using this regex, 是的,您可以使用此正则表达式以积极的眼光捕获性别代码,
(?=(..))(\d{6})(\d{1}[ABDE])(\d{3})\.(\d{3})
This is named groups regex but will only work in Chrome browser 这被命名为“ groups regex”,但仅适用于Chrome浏览器
and named capture grouping will be available in ECMAScript 2018 and is only supported in Chrome as of now. 和命名的捕获分组将在ECMAScript 2018中可用,并且目前仅在Chrome中受支持。
This JS demo will work in Chrome as that is the only one as of now supporting EcmaScript2018, 此JS演示将在Chrome中运行,因为这是目前支持EcmaScript2018的唯一演示,
const validateMpn = (mpn) => { const regex = /(?=(?<gender_code>\\d\\d))(?<style>\\d{6})(?<width>\\d{1}[ABDE])(?<color_code>\\d{3})\\.(?<size_code>\\d{3})/gi const match = regex.exec(mpn) if (!match) { return null } return match.groups } const str1 = '1102961D048.075' const str2 = '1200322A001.085' const match1 = validateMpn(str1) const match2 = validateMpn(str2) console.log(match1) console.log(match2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.