简体   繁体   English

下划线前后的JavaScript匹配

[英]JavaScript match before and after underscore

I have an array in JavaScript. 我在JavaScript中有一个数组。 Items in the array are like below: 数组中的项目如下:

var people = new Array("michael_51", "mark_57", "graham_44", "paul_22"); var people = new Array(“ michael_51”,“ mark_57”,“ graham_44”,“ paul_22”);

I would really like to split the elements before and after the underscore and then use them in a loop. 我真的很想在下划线之前和之后拆分元素,然后在循环中使用它们。

start loop...
(1st Match) - (2nd Match)
end loop...

First in the loop match would be (michael) and second would be (51) etc. 循环匹配中的第一个是(michael),第二个是(51),依此类推。

Many thanks. 非常感谢。

Regex is not really necessary (apart from the split ): 正则表达式实际上不是必需的(除了split ):

var people = new Array("michael_51", "mark_57", "graham_44", "paul_22");

for(var i = 0; i < people.length; i++) {
    var data = people[i].split("_");
    alert("(1st Match): "+data[0]+" - (2nd Match): "+data[1]);
}

Try demo here . 在这里尝试演示

var people = new Array("michael_51", "mark_57", "graham_44", "paul_22");

for (p in people) {
    console.log(people[p].split('_'));
}

See: 看到:

http://jsfiddle.net/WgEUN/ http://jsfiddle.net/WgEUN/

var i, cleanPeople = [];
for (i = 0; i < people.length; i++) {
    cleanPeople.push(people[i].split('_'));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM