正则表达式匹配第二个下划线后的所有内容
regex to match everything after second underscore
我有这个数组
var collUnlocked = ['GRAVEL_1', 'GRAVEL_3', 'GRAVEL_2', 'GRAVEL_5', 'GRAVEL_4', 'GRAVEL_10', 'GRAVEL_-1', 'LOG:3_1', 'LOG:3_-1', 'LOG:3_3', 'LOG:3_4', 'LOG_2_6', 'LOG_2_2', 'LOG_2_3', 'LOG_2_-1']
我想获取姓名(例如 LOG_2)和最后一个数字(例如 -1)
示例:
['LOG:3_4'、'GRAVEL_-1'、'LOG_2_-1']
变成 -> [['LOG:3','4'], ['GRAVEL', '-1'], ['LOG_2', '-1']]
编辑:我正在使用此代码获取姓名,但我无法获取最后一个号码
collName = x.match('(.*\_)')[0];
collName = collName.slice(0, -1);
完整代码 rn:
function testingAll() {
var collUnlocked = ['GRAVEL_1', 'GRAVEL_3', 'GRAVEL_2', 'GRAVEL_5', 'GRAVEL_4', 'GRAVEL_10', 'GRAVEL_-1', 'LOG:3_1', 'LOG:3_-1', 'LOG:3_3', 'LOG:3_4', 'LOG_2_6', 'LOG_2_2', 'LOG_2_3', 'LOG_2_-1']
getCollLevelssss(collUnlocked);
}
function getCollLevelssss(collUnlocked) {
var collName;
var collLevel;
collUnlocked.forEach(x => {
collName = x.match('(.*\_)')[0];
collName = collName.slice(0, -1);
collLevel = x.match(''); //need regex right here
console.log(x + ' | ' + collName + ' = ' + collLevel);
});
}
在您的目标中,以下方法如何?此示例脚本使用 split()
.
示例脚本:
var collUnlocked = ['GRAVEL_1', 'GRAVEL_3', 'GRAVEL_2', 'GRAVEL_5', 'GRAVEL_4', 'GRAVEL_10', 'GRAVEL_-1', 'LOG:3_1', 'LOG:3_-1', 'LOG:3_3', 'LOG:3_4', 'LOG_2_6', 'LOG_2_2', 'LOG_2_3', 'LOG_2_-1']
var res = collUnlocked.map(e => {
var temp = e.split("_");
var last = temp.pop();
return [temp.join("_"), last];
});
console.log(res)
当此脚本为运行时,得到如下结果
[
[ 'GRAVEL', '1' ],
[ 'GRAVEL', '3' ],
[ 'GRAVEL', '2' ],
[ 'GRAVEL', '5' ],
[ 'GRAVEL', '4' ],
[ 'GRAVEL', '10' ],
[ 'GRAVEL', '-1' ],
[ 'LOG:3', '1' ],
[ 'LOG:3', '-1' ],
[ 'LOG:3', '3' ],
[ 'LOG:3', '4' ],
[ 'LOG_2', '6' ],
[ 'LOG_2', '2' ],
[ 'LOG_2', '3' ],
[ 'LOG_2', '-1' ]
]
参考:
分裂与重组
function myfunk() {
let a = ['GRAVEL_1', 'GRAVEL_3', 'GRAVEL_2', 'GRAVEL_5', 'GRAVEL_4', 'GRAVEL_10', 'GRAVEL_-1', 'LOG:3_1', 'LOG:3_-1', 'LOG:3_3', 'LOG:3_4', 'LOG_2_6', 'LOG_2_2', 'LOG_2_3', 'LOG_2_-1'];
let b = a.reduce((a,c) => {
let t = c.split('_');
if(t.length == 2) {
a.arr.push(`${t[0]},${t[1]}`);
} else if(t.length == 3) {
a.arr.push(`${t[0]}_${t[1]},${t[2]}`);
}
return a;
},{arr:[]}).arr;
Logger.log(JSON.stringify(b));
}
Execution log
7:10:50 PM Notice Execution started
7:10:50 PM Info ["GRAVEL,1","GRAVEL,3","GRAVEL,2","GRAVEL,5","GRAVEL,4","GRAVEL,10","GRAVEL,-1","LOG:3,1","LOG:3,-1","LOG:3,3","LOG:3,4","LOG_2,6","LOG_2,2","LOG_2,3","LOG_2,-1"]
7:10:51 PM Notice Execution completed
你可以拆分 negative look ahead:
const collUnlocked = ['GRAVEL_1', 'GRAVEL_3', 'GRAVEL_2', 'GRAVEL_5', 'GRAVEL_4', 'GRAVEL_10', 'GRAVEL_-1', 'LOG:3_1', 'LOG:3_-1', 'LOG:3_3', 'LOG:3_4', 'LOG_2_6', 'LOG_2_2', 'LOG_2_3', 'LOG_2_-1']
const res = collUnlocked.map(e => e.split(/_(?!.*_.*)/));
console.log(res)
/_(?!.*_.*)
- _
后面没有任何 .*_.*
(_
周围的任何字符)
我有这个数组
var collUnlocked = ['GRAVEL_1', 'GRAVEL_3', 'GRAVEL_2', 'GRAVEL_5', 'GRAVEL_4', 'GRAVEL_10', 'GRAVEL_-1', 'LOG:3_1', 'LOG:3_-1', 'LOG:3_3', 'LOG:3_4', 'LOG_2_6', 'LOG_2_2', 'LOG_2_3', 'LOG_2_-1']
我想获取姓名(例如 LOG_2)和最后一个数字(例如 -1)
示例: ['LOG:3_4'、'GRAVEL_-1'、'LOG_2_-1']
变成 -> [['LOG:3','4'], ['GRAVEL', '-1'], ['LOG_2', '-1']]
编辑:我正在使用此代码获取姓名,但我无法获取最后一个号码
collName = x.match('(.*\_)')[0];
collName = collName.slice(0, -1);
完整代码 rn:
function testingAll() {
var collUnlocked = ['GRAVEL_1', 'GRAVEL_3', 'GRAVEL_2', 'GRAVEL_5', 'GRAVEL_4', 'GRAVEL_10', 'GRAVEL_-1', 'LOG:3_1', 'LOG:3_-1', 'LOG:3_3', 'LOG:3_4', 'LOG_2_6', 'LOG_2_2', 'LOG_2_3', 'LOG_2_-1']
getCollLevelssss(collUnlocked);
}
function getCollLevelssss(collUnlocked) {
var collName;
var collLevel;
collUnlocked.forEach(x => {
collName = x.match('(.*\_)')[0];
collName = collName.slice(0, -1);
collLevel = x.match(''); //need regex right here
console.log(x + ' | ' + collName + ' = ' + collLevel);
});
}
在您的目标中,以下方法如何?此示例脚本使用 split()
.
示例脚本:
var collUnlocked = ['GRAVEL_1', 'GRAVEL_3', 'GRAVEL_2', 'GRAVEL_5', 'GRAVEL_4', 'GRAVEL_10', 'GRAVEL_-1', 'LOG:3_1', 'LOG:3_-1', 'LOG:3_3', 'LOG:3_4', 'LOG_2_6', 'LOG_2_2', 'LOG_2_3', 'LOG_2_-1']
var res = collUnlocked.map(e => {
var temp = e.split("_");
var last = temp.pop();
return [temp.join("_"), last];
});
console.log(res)
当此脚本为运行时,得到如下结果
[ [ 'GRAVEL', '1' ], [ 'GRAVEL', '3' ], [ 'GRAVEL', '2' ], [ 'GRAVEL', '5' ], [ 'GRAVEL', '4' ], [ 'GRAVEL', '10' ], [ 'GRAVEL', '-1' ], [ 'LOG:3', '1' ], [ 'LOG:3', '-1' ], [ 'LOG:3', '3' ], [ 'LOG:3', '4' ], [ 'LOG_2', '6' ], [ 'LOG_2', '2' ], [ 'LOG_2', '3' ], [ 'LOG_2', '-1' ] ]
参考:
分裂与重组
function myfunk() {
let a = ['GRAVEL_1', 'GRAVEL_3', 'GRAVEL_2', 'GRAVEL_5', 'GRAVEL_4', 'GRAVEL_10', 'GRAVEL_-1', 'LOG:3_1', 'LOG:3_-1', 'LOG:3_3', 'LOG:3_4', 'LOG_2_6', 'LOG_2_2', 'LOG_2_3', 'LOG_2_-1'];
let b = a.reduce((a,c) => {
let t = c.split('_');
if(t.length == 2) {
a.arr.push(`${t[0]},${t[1]}`);
} else if(t.length == 3) {
a.arr.push(`${t[0]}_${t[1]},${t[2]}`);
}
return a;
},{arr:[]}).arr;
Logger.log(JSON.stringify(b));
}
Execution log
7:10:50 PM Notice Execution started
7:10:50 PM Info ["GRAVEL,1","GRAVEL,3","GRAVEL,2","GRAVEL,5","GRAVEL,4","GRAVEL,10","GRAVEL,-1","LOG:3,1","LOG:3,-1","LOG:3,3","LOG:3,4","LOG_2,6","LOG_2,2","LOG_2,3","LOG_2,-1"]
7:10:51 PM Notice Execution completed
你可以拆分 negative look ahead:
const collUnlocked = ['GRAVEL_1', 'GRAVEL_3', 'GRAVEL_2', 'GRAVEL_5', 'GRAVEL_4', 'GRAVEL_10', 'GRAVEL_-1', 'LOG:3_1', 'LOG:3_-1', 'LOG:3_3', 'LOG:3_4', 'LOG_2_6', 'LOG_2_2', 'LOG_2_3', 'LOG_2_-1']
const res = collUnlocked.map(e => e.split(/_(?!.*_.*)/));
console.log(res)
/_(?!.*_.*)
- _
后面没有任何 .*_.*
(_
周围的任何字符)