最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - I want to get value before and after colon with regex - Stack Overflow

matteradmin8PV0评论

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

Post a comment

comment list (0)

  1. No comments so far