我有(例如)字符串let abc = 'Jonny_Name'
,所以如果我想检查,这是不是名字,我会检查:
let isName = abc.split('_')[1];
isName === 'Name' ? `your name is ${abc.split('_')[0]}` : 'not name';
但我遇到这种情况,当我有像这样的字符串时let cba = 'Jonny_Great_Dude_Name'
在这种情况下,我以这种方式检查名称:
let isName = cba.split('_').pop(-1);
isName === 'Name' ? `your name is ${cba.splitOnLast('_')[0]}` : 'not name'
这工作正常,但我没有找到任何关于splitOnLast
方法。这个方法在浏览器兼容方面有问题吗?我可以为此做一些替代方案吗?我想获取所有元素,没有最后一个..
UPD. in console it works fine, but in project I have error message like item.splitOnLast is not a function
UPD。就像第一个例子一样Jonny_Name
成为'Jonny'
, 期望Jonny_Great_Dude_Name
将Jonny_Great_Dude
const abc = 'Jonny_Great_Dude_Name';
const splitted = abc.split(/_/);
const [other, name] = [splitted.pop(), splitted.join('_')];
console.log({name:name, isName: other == 'Name'});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)