我的字符串格式为“ [111-11]文本在这里,数字为111,[222-22-22]; 333-33文本在这里”,我想对其进行解析,以便获得代码[111-11] ,[222-22-22],[333-33]及其对应的文本。 除了代码xxx-xx或xxx-xx-xx外,我没有固定的分离器。
我以这种方式尝试过,但是在desc部分却无法获得数字。 \ D将获得数字以外的任何值。
let text = "[111-11] text here with digits 111, [222-22-22]; 333-33 text here";
let codes=[];
let result = text.replace(/(\d{3}(-\d{2})+)(\D*)/g,(str, code, c, desc) => {
desc = desc.trim().replace(/[\[\]']+/g,'');
if (code) codes.push({'code':code.trim(),'desc': desc});
return str;
}); //parse and split codes
最后,我想要这种风格的结果:
[{code:'111-11', desc:'text here with digits 111'},
{code:'222-22-22', desc:''},
{code:'333-33', desc:'text here'}]
我非常感谢您的帮助。
答案 0 :(得分:0)
您可以搜索方括号值和成组的以下文本,并以正向查找字符串的部分或结尾。然后解构字符串并推送所需的对象。
const regex = /\[?(\d{3}(-\d\d)+)\]?(.*?)(?=\[?\d{3}(-\d\d)+\]?|$)/gm;
const str = `[111-11] text here with digits 111, [222-22-22]; 333-33 text here`;
var m,
code, desc,
result= [];
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
({ 1: code, 3: desc } = m);
result.push({ code, desc })
}
console.log(result);
答案 1 :(得分:0)
另一种方法:
const Sep = ','
const PatSep = /[,;]/g
// skippng first spaces, then getting the numbers (and ignoring the brackets
// if presents), then gets rest
const PatPart = /^\s*\[?(\d{3}(-\d{2})+)]?(.*)$/
const src =
"[111-11] text here with digits 111, [222-22-22]; 333-33 text here"
const parse = src => {
// to avoir differents terminations
const normalized = src.replace (PatSep, Sep)
return normalized.split (Sep).reduce((acc, part) => {
// getting code and desc from part
const [_, code, __, desc] = part.match (PatPart)
// accumulating in desired array
return [
...acc,
{code, desc}
]
}, [])
}
console.log(parse (src))
;)