Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to get value before and after colon with regex

Here is the example of my string:

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

Here is the array of different values with same type and I need to create an array of values before colon and after colon.

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

I need do the same but with regex can somebody help me?

like image 725
Michael Kronenberg Avatar asked Dec 19 '25 22:12

Michael Kronenberg


1 Answers

You can try that:

([^:\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);
like image 162
Rizwan M.Tuman Avatar answered Dec 22 '25 11:12

Rizwan M.Tuman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!