How to check if last character of a string is a digit/number in plain JavaScript?
(如何检查纯JavaScript中字符串的最后一个字符是否为数字/数字?)
function endsWithNumber( str ){ return str.endsWith(); // HOW TO CHECK IF STRING ENDS WITH DIGIT/NUMBER ??? } var str_1 = 'Pocahontas'; var str_2 = 'R2D2'; if( endsWithNumber( str_1 ) ){ console.log( str_1 + 'ends with a number' ); } else { console.log( str_1 + 'does NOT end with a number' ); } if( endsWithNumber( str_2 ) ){ console.log( str_2 + 'ends with a number' ); } else { console.log( str_2 + 'does NOT end with a number' ); }
Also I would like to know what would be the most fastest way?
(我也想知道最快的方法是什么?)
I guess it may sound ridiculous :D but in my usecase I will need this method veeery often so I think it could make a difference. (我想这听起来很荒谬:D,但是在我的用例中,我经常需要这种方法,所以我认为这可能有所作为。)
ask by RE666 translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…