gpt4 book ai didi

javascript - 我想用正则表达式在冒号前后获得值(value)

转载 作者:行者123 更新时间:2023-11-29 10:32:33 25 4
gpt4 key购买 nike

这是我的字符串的例子:

or id:bBkeed 
or name:Michael
or surname:Kronenberg

这是具有相同类型的不同值的数组,我需要在冒号之前和冒号之后创建一个值数组。

const afterDOT = splitedValue[index].substring(splitedValue[index].indexOf(':') + 1);
const beforeDOT = splitedValue[index].substring(0, splitedValue[index].indexOf(':'));
afterDOTS.push(afterDOT);
beforeDOTS.push(beforeDOT);

我需要做同样的事情,但是有人可以用正则表达式帮助我吗?

最佳答案

你可以试试:

([^:\s]+):([^:\s]+)

Explanation

const regex = /([^:\s]+):([^:\s]+)/g;
const str = `id:bBkeed or name:Michael or surname:Kronenberg`;
let m;

var before=[];
var after=[];

while ((m = regex.exec(str)) !== null) {
before.push(m[1]);
after.push(m[2]);
}
console.log(before);
console.log(after);

关于javascript - 我想用正则表达式在冒号前后获得值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42606526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com