在 JavaScript 中获取字符串中破折号后面的所有内容
2022-08-30 01:04:46
在IE和Firefox中都有效的最干净的方法是什么?
我的字符串看起来像这样sometext-20202
现在,破折号后面的 和 整数可以具有不同的长度。sometext
我应该只使用 和 索引 还是有其他方法?substring
在IE和Firefox中都有效的最干净的方法是什么?
我的字符串看起来像这样sometext-20202
现在,破折号后面的 和 整数可以具有不同的长度。sometext
我应该只使用 和 索引 还是有其他方法?substring
我该怎么做:
// function you can use:
function getSecondPart(str) {
return str.split('-')[1];
}
// use the function:
alert(getSecondPart("sometext-20202"));
我更喜欢的解决方案是:
const str = 'sometext-20202';
const slug = str.split('-').pop();
您的结果在哪里slug