Angular 正则表达式拆分字符串 1x asd..... 2x asd
Angular Regex Split String 1x asd..... 2x asd
我正在尝试将字符串拆分为 [0-9]+ [xX] | [0-9]+[xX],字符串示例:
10 x source 4 50° on booms (2 downstage left and right, 2 upstage left and right) 2x festoons (mentioned above) 4x 1kx Fresnel backlight in L712 4x SL 15/32 (3x L443, 4x R119) – all four of which are specifically focused specials 1x Edison bulb suspended from grid
我想在 angular 中使用管道将文本分成单独的行,以显示如下:
10 x source 4 50° on booms (2 downstage left and right, 2 upstage left and right)
2x festoons (mentioned above)
4x 1kx Fresnel backlight in L712
4x SL 15/32 (3x L443, 4x R119) – all four of which are specifically focused specials
1x Edison bulb suspended from grid
我尝试使用以下正则表达式,但它在正则表达式中包含了下一个 1x。
/([0-9]+[xX] [a-zA-z].*)(?:([0-9]+[xX]))/gm
Output: 10x source 4 50° on booms (2 downstage left and right, 2 upstage left and right) 2x festoons (mentioned above) 4x 1kx Fresnel backlight in L712 4x SL 15/32 (3x L443, 4x R119) – all four of which are specifically focused specials 1x
我也希望能够添加第一句中提到的选项来检查两种情况。
- 1x asd....
- 1 x asd....
然后我将使用 Angular 中的正则表达式拆分字符串。
你可以使用
/\d+\s*x\b(?:\([^()]*\)|[^()])*?(?=\d+x|$)/gi
见regex demo。 详情:
\d+
- 一位或多位数字
\s*
- 零个或多个空格
x\b
- x
和单词边界
(?:\([^()]*\)|[^()])*?
- (
的零个或多个(但尽可能少)序列 + (
和 )
+ [= 以外的零个或多个字符19=] 或除 (
和 )
之外的任何字符
(?=\d+x|$)
- 与紧跟一个或多个数字和 x
或字符串末尾的位置匹配的正前瞻。
查看 JavaScript 演示:
const text = "10 x source 4 50° on booms (2 downstage left and right, 2 upstage left and right) 2x festoons (mentioned above) 4x 1kx Fresnel backlight in L712 4x SL 15/32 (3x L443, 4x R119) – all four of which are specifically focused specials 1x Edison bulb suspended from grid";
const re = /\d+\s*x\b(?:\([^()]*\)|[^()])*?(?=\d+x|$)/gi;
document.body.innerHTML = "<pre>" + JSON.stringify(text.match(re), 0, 4) + "</pre>"
我正在尝试将字符串拆分为 [0-9]+ [xX] | [0-9]+[xX],字符串示例:
10 x source 4 50° on booms (2 downstage left and right, 2 upstage left and right) 2x festoons (mentioned above) 4x 1kx Fresnel backlight in L712 4x SL 15/32 (3x L443, 4x R119) – all four of which are specifically focused specials 1x Edison bulb suspended from grid
我想在 angular 中使用管道将文本分成单独的行,以显示如下:
10 x source 4 50° on booms (2 downstage left and right, 2 upstage left and right)
2x festoons (mentioned above)
4x 1kx Fresnel backlight in L712
4x SL 15/32 (3x L443, 4x R119) – all four of which are specifically focused specials
1x Edison bulb suspended from grid
我尝试使用以下正则表达式,但它在正则表达式中包含了下一个 1x。
/([0-9]+[xX] [a-zA-z].*)(?:([0-9]+[xX]))/gm
Output: 10x source 4 50° on booms (2 downstage left and right, 2 upstage left and right) 2x festoons (mentioned above) 4x 1kx Fresnel backlight in L712 4x SL 15/32 (3x L443, 4x R119) – all four of which are specifically focused specials 1x
我也希望能够添加第一句中提到的选项来检查两种情况。
- 1x asd....
- 1 x asd....
然后我将使用 Angular 中的正则表达式拆分字符串。
你可以使用
/\d+\s*x\b(?:\([^()]*\)|[^()])*?(?=\d+x|$)/gi
见regex demo。 详情:
\d+
- 一位或多位数字\s*
- 零个或多个空格x\b
-x
和单词边界(?:\([^()]*\)|[^()])*?
-(
的零个或多个(但尽可能少)序列 +(
和)
+ [= 以外的零个或多个字符19=] 或除(
和)
之外的任何字符
(?=\d+x|$)
- 与紧跟一个或多个数字和x
或字符串末尾的位置匹配的正前瞻。
查看 JavaScript 演示:
const text = "10 x source 4 50° on booms (2 downstage left and right, 2 upstage left and right) 2x festoons (mentioned above) 4x 1kx Fresnel backlight in L712 4x SL 15/32 (3x L443, 4x R119) – all four of which are specifically focused specials 1x Edison bulb suspended from grid";
const re = /\d+\s*x\b(?:\([^()]*\)|[^()])*?(?=\d+x|$)/gi;
document.body.innerHTML = "<pre>" + JSON.stringify(text.match(re), 0, 4) + "</pre>"