Answers:
我将如何做:
// function you can use:
function getSecondPart(str) {
return str.split('-')[1];
}
// use the function:
alert(getSecondPart("sometext-20202"));
get everything after the dash in a string in javascript在这种情况下会失败。
我更喜欢的解决方案是:
const str = 'sometext-20202';
const slug = str.split('-').pop();
slug你的结果在哪里
Mozilla和IE均支持AFAIK substring()和AFAIK indexOf()。但是,请注意,某些浏览器的早期版本(尤其是Netscape / Opera)可能不支持substr()。
您的帖子表明您已经知道如何使用substring()和进行操作indexOf(),因此我不会发布代码示例。
myString.split('-').splice(1).join('-')
myString.split('-').splice(-1).join('-')
sometext-20202-303?