有没有简单的方法从自由格式文本中提取值?
我正试图从中提取数据:
"The process started from ITEM: ASDE3423423, Item Group: A_GROUP, ITEM Function: ['/A/B/C'], Price Code: average."
我需要基于密钥的':'之后的o / p(例如ITEM,Item Group)
ITEM = ASDE3423423
Item Group = A_GROUP
ITEM Function = ['/A/B/C']
Price Code = average
我正在使用:
ITEM:\s*([^,]+)
Item Group:\s*([^,]+)
ITEM Function:\s*([^,]+)
Price Code:\s*([^,]+)
并使用':'拆分字符串并获取给定键的str [1](例如ITEM,Item Group)
有没有更好的方法呢?
答案 0 :(得分:2)
不是返回完整匹配并拆分它而是返回捕获组。
// DATA STRING
var str = 'The process started from ITEM: ASDE3423423, Item Group: A_GROUP, ITEM Function: [\'/A/B/C\'], Price Code: average.',
// REGEX
reg = /ITEM:\s*([^,]+)|Item Group:\s*([^,]+)|ITEM Function:\s*([^,]+)|Price Code:\s*([^,]+)/g;
// GET MATCH
match = reg.exec(str);
// LOOP MATCH
while(match != null) {
// LOG MATCH
console.log(match[0])
// TRY FOR NEXT MATCH
match = reg.exec(str);
}