如何根据下面的示例字符串数组对字符串进行切片
[
'PAP02-123-101-000-000-000-000-0000-SP-01-01',
'PAP02-123-102-000-000-000-000-0000-SUN-01-02',
'PAP02-123-102-000-000-000-000-0000-SUT-01-01'
]
这是最终的输出
SP-01-01
SUN-01-02
SUT-01-01
这是我能想到的实现所需输出的方法
const str = 'PAP02-123-102-000-000-000-000-0000-SUN-01-02';
const newStr = str.substring(str.indexof(str.match(/^(SP|SUP|SUN)$/)))
最佳答案
您可以拆分
、反转
和解构
。
此方法适用于任何字符串,因为无论每个标记的长度如何,都会获取最后三个出现的位置。
var array = [
'PAP02-123-101-000-000-000-000-0000-SP-01-01',
'PAP02-123-102-000-000-000-000-0000-SUN-01-02',
'PAP02-123-102-000-000-000-000-0000-SUT-01-01'
];
var result = array.map((s) => {
let [a, b, c] = s.split('-').reverse();
return `${c}-${b}-${a}`;
});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
关于javascript - 根据字符串列表数组动态查找indexof,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48895998/