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

前端 未结 7 1537
挽巷
挽巷 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:15

    in regex try this:

    function getAndIncrementLastNumber(str){
       var myRe = /\d+[0-9]{0}$/g;  
       var myArray = myRe.exec(str);
       return parseInt(myArray[0])+1;​
    }
    

    demo : http://jsfiddle.net/F9ssP/1/

提交回复
热议问题