Get the first integers in a string with JavaScript

前端 未结 5 1676
谎友^
谎友^ 2020-12-02 10:23

I have a string in a loop and for every loop, it is filled with texts the looks like this:

\"123 hello everybody 4\"
\"4567 stuff is fun 67\"
\"12368 more st         


        
5条回答
  •  爱一瞬间的悲伤
    2020-12-02 11:16

    This replace method with a simple regular expression ([^\d].*):

    '123 your 1st string'.replace( /[^\d].*/, '' );
    // output: "123"
    

    remove everything without the first digits.

提交回复
热议问题