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
2 Answers
Reset to default 3You 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
2 Answers
Reset to default 3You 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
版权声明:本文标题:javascript - I want to get value before and after colon with regex - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745651924a2161370.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论