【发布时间】:2021-12-10 05:50:46
【问题描述】:
我在不使用 Split 或任何其他类似功能的情况下使用正则表达式来获取字符串的各个部分,这是我的场景:
我有这个文本U:BCCNT.3;GO,我想把不同的部分分开,但是中间的符号我已经设法用这个正则表达式得到第一个/(.+):/.exec(value),这给了我第一个单词直到冒号(: ) 这些是值的不同变体
第二节BCCNT
BCCNT.3;GO -> 没有U:,所以字符串也可能不包含冒号,所以第二部分的逻辑是any text that is between : and . or any text ending with . and nothing infront
第三节.3-> any text starting with a . and ending with nothing or anytext staring with a . and ending with a ; semicolon
第四节;GO->any text starting with a ; and ending with nothing
编辑 最好在单独的变量上,比如
const sectionOne = regex.exec(value);
const sectionTwo = regex.exec(value);
const sectionThree = regex.exec(value);
const sectionFour = regex.exec(value);
并且哪个值与模式不匹配,变量将只是 undefined 或 null 或任何空字符串
【问题讨论】:
标签: javascript regex string