Javascript string.match后面有特定的前缀和文本



也许有人可以帮助我从字符串中获取零件数据。

let input1: string = "any string before specialPrefix: ['content1', 'content2'] and any other text after";  
let input2: string = "any string before specialPrefix:['content1', 'content2'] and any other text after";  
let input3: string = "any string before specialPrefix: 'content1', 'content2' and any other text after";  

预期

输出 1, 输出 2: 具有两个值的数组,"内容 1"和"内容 2"。
输出 3:空数组

您可以使用

/specialPrefix:s*([.*?])/

let inputs = ["any string before specialPrefix: ['content1', 'content2'] and any other text after","any string before specialPrefix:['content1', 'content2'] and any other text after","any string before specialPrefix: 'content1', 'content2' and any other text after"];  
var result = inputs.map(str => (/specialPrefix:s*([.*?])/.exec(str) || [])[1]);
console.log(result);

相关内容

最新更新