gpt4 book ai didi

javascript - 如何获取字符串中的最后一个值

转载 作者:行者123 更新时间:2023-11-30 13:38:33 26 4
gpt4 key购买 nike

我知道我可以通过这样做来解析出 int

parseInt($('#count_of_stations').html())

如果值类似于“2stations”,它将返回 2,但现在我需要以这种格式获取整数

one1
two2
three3
four4
five5

我怎样才能得到数字

最佳答案

如果数字总是0-9,你可以使用slice()具有负偏移量:

var num = $('#count_of_stations').text().slice(-1);

或者,如果字符串的开头直到数字 始终相同,您可以使用具有正偏移量的slice():

"mystring1".slice(8); // -> 1
"mystring12".slice(8); // -> 12

如果前两个建议不符合您的要求,您可以使用match()使用正则表达式:

var num = $('#count_of_stations').text().match(/\d+$/)[0];

关于javascript - 如何获取字符串中的最后一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3541539/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com