如何在正则表达式 String.split() 中包含分隔符?
How can I include the delimiter with regex String.split()?
我需要从 GS1 UDI 格式字符串中解析令牌:
"(20)987111(240)A(10)ABC123(17)2022-04-01(21)888888888888888"
我想在 "(nnn)"
上用正则表达式拆分该字符串,并将分隔符包含在拆分值中,如下所示:
[ "(20)987111", "(240)A", "(10)ABC123", "(17)2022-04-01", "(21)888888888888888" ]
下面是带有示例的 JSFiddle,但如果您想在此处查看它:
// This includes the delimiter match in the results, but I want the delimiter included WITH the value
// after it, e.g.: ["(20)987111", ...]
str = "(20)987111(240)A(10)ABC123(17)2022-04-01(21)888888888888888";
console.log(str.split(/(\(\d{2,}\))/).filter(Boolean))
// Result: ["(20)", "987111", "(240)", "A", "(10)", "ABC123", "(17)", "2022-04-01", "(21)", "888888888888888"]
// If I include a pattern that should (I think) match the content following the delimiter I will
// only get a single result that is the full string:
str = "(20)987111(240)A(10)ABC123(17)2022-04-01(21)888888888888888";
console.log(str.split(/(\(\d{2,}\)\W+)/).filter(Boolean))
// Result: ["(20)987111(240)A(10)ABC123(17)2022-04-01(21)888888888888888"]
// I think this is because I'm effectively mathching the entire string, hence a single result.
// So now I'll try to match only up to the start of the next "(":
str = "(20)987111(240)A(10)ABC123(17)2022-04-01(21)888888888888888";
console.log(str.split(/(\(\d{2,}\)(^\())/).filter(Boolean))
// Result: ["(20)987111(240)A(10)ABC123(17)2022-04-01(21)888888888888888"]
我找到并阅读了 this question,但是那里的示例是匹配的文字,我使用字符 类 并得到不同的结果。
我无法创建一个正则表达式模式来提供我想要的内容。这是我尝试过的一些事情的 JSFiddle:https://jsfiddle.net/6bogpqLy/
我不能保证输入字符串中“应用程序标识符”的顺序,因此,与命名捕获匹配不是一个有吸引力的选择。
而不是 split
使用 match
创建数组。然后找到 1) 括号中的数字,后跟可能包含数字、字母或连字符的组,然后 2) 将整个查询分组。
(PS。我经常发现像 Regex101 这样的网站在测试开发环境之外的表达式时确实很有帮助。)
const re = /(\(\d+\)[\d\-A-Z]+)/g;
const str = '(20)987111(240)A(10)ABC123(17)2022-04-01(21)888888888888888';
console.log(str.match(re));
您可以使用 zero-length 前瞻断言在括号元素后面的位置进行拆分:
const text = "(20)987111(240)A(10)ABC123(17)2022-04-01(21)888888888888888"
const parts = text.split(/(?=\(\d+\))/)
console.log(parts)
我需要从 GS1 UDI 格式字符串中解析令牌:
"(20)987111(240)A(10)ABC123(17)2022-04-01(21)888888888888888"
我想在 "(nnn)"
上用正则表达式拆分该字符串,并将分隔符包含在拆分值中,如下所示:
[ "(20)987111", "(240)A", "(10)ABC123", "(17)2022-04-01", "(21)888888888888888" ]
下面是带有示例的 JSFiddle,但如果您想在此处查看它:
// This includes the delimiter match in the results, but I want the delimiter included WITH the value
// after it, e.g.: ["(20)987111", ...]
str = "(20)987111(240)A(10)ABC123(17)2022-04-01(21)888888888888888";
console.log(str.split(/(\(\d{2,}\))/).filter(Boolean))
// Result: ["(20)", "987111", "(240)", "A", "(10)", "ABC123", "(17)", "2022-04-01", "(21)", "888888888888888"]
// If I include a pattern that should (I think) match the content following the delimiter I will
// only get a single result that is the full string:
str = "(20)987111(240)A(10)ABC123(17)2022-04-01(21)888888888888888";
console.log(str.split(/(\(\d{2,}\)\W+)/).filter(Boolean))
// Result: ["(20)987111(240)A(10)ABC123(17)2022-04-01(21)888888888888888"]
// I think this is because I'm effectively mathching the entire string, hence a single result.
// So now I'll try to match only up to the start of the next "(":
str = "(20)987111(240)A(10)ABC123(17)2022-04-01(21)888888888888888";
console.log(str.split(/(\(\d{2,}\)(^\())/).filter(Boolean))
// Result: ["(20)987111(240)A(10)ABC123(17)2022-04-01(21)888888888888888"]
我找到并阅读了 this question,但是那里的示例是匹配的文字,我使用字符 类 并得到不同的结果。
我无法创建一个正则表达式模式来提供我想要的内容。这是我尝试过的一些事情的 JSFiddle:https://jsfiddle.net/6bogpqLy/
我不能保证输入字符串中“应用程序标识符”的顺序,因此,与命名捕获匹配不是一个有吸引力的选择。
而不是 split
使用 match
创建数组。然后找到 1) 括号中的数字,后跟可能包含数字、字母或连字符的组,然后 2) 将整个查询分组。
(PS。我经常发现像 Regex101 这样的网站在测试开发环境之外的表达式时确实很有帮助。)
const re = /(\(\d+\)[\d\-A-Z]+)/g;
const str = '(20)987111(240)A(10)ABC123(17)2022-04-01(21)888888888888888';
console.log(str.match(re));
您可以使用 zero-length 前瞻断言在括号元素后面的位置进行拆分:
const text = "(20)987111(240)A(10)ABC123(17)2022-04-01(21)888888888888888"
const parts = text.split(/(?=\(\d+\))/)
console.log(parts)