如何使用正则表达式组拆分键和值
How to use regex groups to split key & values
我想提取一些数据以在 JS 中构建对象,但我找不到如何使用正则表达式。
在 regex101 上,下面的正则表达式似乎适合,但它不在我的代码中...
这是数据类型:
"TEST_firstname:john_lastname:doe_age:45"
我想提取键和值(键在 "_"
和 ":"
之间,值在 ":"
和 "_"
之间
我试过这个:(?<key>(?<=\_)(.*?)(?=\:))|(?<value>(?<=\:)(.*?)((?=\_)|$))
有人可以帮我找到合适的正则表达式吗?
使用
/([^_:]+):([^_]+)/g
解释
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
[^_:]+ any character except: '_', ':' (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of
--------------------------------------------------------------------------------
: ':'
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
[^_]+ any character except: '_' (1 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of
JavaScript代码:
const regex = /([^_:]+):([^_]+)/g;
const str = `TEST_firstname:john_lastname:doe_age:45`;
while ((m = regex.exec(str)) !== null) {
console.log(`${m[1]} >>> ${m[2]}`);
}
使用String.prototype.matchAll and Array.prototype.reduce将其缩减为key:value对
的对象
const s = "TEST_firstname:john_lastname:doe_age:45";
const m = s.matchAll(/([^:_]+):([^_]+)/g);
const user = [...m].reduce((ob, [m,k,v]) => (ob[k] = v, ob), {});
console.log(user);
我想提取一些数据以在 JS 中构建对象,但我找不到如何使用正则表达式。
在 regex101 上,下面的正则表达式似乎适合,但它不在我的代码中...
这是数据类型:
"TEST_firstname:john_lastname:doe_age:45"
我想提取键和值(键在 "_"
和 ":"
之间,值在 ":"
和 "_"
我试过这个:(?<key>(?<=\_)(.*?)(?=\:))|(?<value>(?<=\:)(.*?)((?=\_)|$))
有人可以帮我找到合适的正则表达式吗?
使用
/([^_:]+):([^_]+)/g
解释
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
[^_:]+ any character except: '_', ':' (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of
--------------------------------------------------------------------------------
: ':'
--------------------------------------------------------------------------------
( group and capture to :
--------------------------------------------------------------------------------
[^_]+ any character except: '_' (1 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
) end of
JavaScript代码:
const regex = /([^_:]+):([^_]+)/g;
const str = `TEST_firstname:john_lastname:doe_age:45`;
while ((m = regex.exec(str)) !== null) {
console.log(`${m[1]} >>> ${m[2]}`);
}
使用String.prototype.matchAll and Array.prototype.reduce将其缩减为key:value对
的对象const s = "TEST_firstname:john_lastname:doe_age:45";
const m = s.matchAll(/([^:_]+):([^_]+)/g);
const user = [...m].reduce((ob, [m,k,v]) => (ob[k] = v, ob), {});
console.log(user);