admin管理员组

文章数量:1026938

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?

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?

Share Improve this question edited Mar 5, 2017 at 9:33 Mustofa Rizwan 10.5k2 gold badges29 silver badges46 bronze badges asked Mar 5, 2017 at 9:15 Michael KronenbergMichael Kronenberg 1042 silver badges10 bronze badges 4
  • Possible duplicate of Learning Regular Expressions – Biffen Commented Mar 5, 2017 at 9:18
  • plz format your example string .. can't distrinct – Mustofa Rizwan Commented Mar 5, 2017 at 9:19
  • 1 By ‘dots’ do you mean colon? – Biffen Commented Mar 5, 2017 at 9:19
  • var x = "id:bKess" or var array = ['id:34', 'surname:kronenberg', 'name:Michael'] – Michael Kronenberg Commented Mar 5, 2017 at 9:22
Add a ment  | 

2 Answers 2

Reset to default 3

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);

You can simply use .match(regex)

var str = "abc:xyz";
var first = str.match(/(.*):/g).pop().replace(":","");
var last = str.match(/:(.*)/g).pop().replace(":","");

console.log("String : " + str, "\nfirst : " + first, "\nlast : " + last);


You can easily extend this for your case. See below :

var string = "abc:xyz id:234 surname:kronenberg";

string.split(" ").forEach(function(str) {
  var first = str.match(/(.*):/g).pop().replace(":", "");
  var last = str.match(/:(.*)/g).pop().replace(":", "");
  console.log("first:" + first, "last:" + last);
});

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?

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?

Share Improve this question edited Mar 5, 2017 at 9:33 Mustofa Rizwan 10.5k2 gold badges29 silver badges46 bronze badges asked Mar 5, 2017 at 9:15 Michael KronenbergMichael Kronenberg 1042 silver badges10 bronze badges 4
  • Possible duplicate of Learning Regular Expressions – Biffen Commented Mar 5, 2017 at 9:18
  • plz format your example string .. can't distrinct – Mustofa Rizwan Commented Mar 5, 2017 at 9:19
  • 1 By ‘dots’ do you mean colon? – Biffen Commented Mar 5, 2017 at 9:19
  • var x = "id:bKess" or var array = ['id:34', 'surname:kronenberg', 'name:Michael'] – Michael Kronenberg Commented Mar 5, 2017 at 9:22
Add a ment  | 

2 Answers 2

Reset to default 3

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);

You can simply use .match(regex)

var str = "abc:xyz";
var first = str.match(/(.*):/g).pop().replace(":","");
var last = str.match(/:(.*)/g).pop().replace(":","");

console.log("String : " + str, "\nfirst : " + first, "\nlast : " + last);


You can easily extend this for your case. See below :

var string = "abc:xyz id:234 surname:kronenberg";

string.split(" ").forEach(function(str) {
  var first = str.match(/(.*):/g).pop().replace(":", "");
  var last = str.match(/:(.*)/g).pop().replace(":", "");
  console.log("first:" + first, "last:" + last);
});

本文标签: javascriptI want to get value before and after colon with regexStack Overflow