Get and replace the last number on a string with JavaScript or jQuery

前端 未结 7 1531
挽巷
挽巷 2021-02-14 11:02

If I have the string:

var myStr = \"foo_0_bar_0\";

and I guess we should have a function called getAndIncrementLastNumber(str)

相关标签:
7条回答
  • 2021-02-14 11:29

    If you want to only get the last number of string, Here is a good way using parseInt()

    if(Stringname.substr(-3)==parseInt(Stringname.substr(-3)))
    var b=Stringname.substr(-3);
    else if(Stringname.substr(-2)==parseInt(Stringname.substr(-2)))
    var b=Stringname.substr(-2);
    else
    var b=Stringname.substr(-1);
    

    It checks and give the correct answer and store it in variable b for 1 digit number and upto 3 digit number. You can make it to any if you got the logic

    0 讨论(0)
提交回复
热议问题