Get the first integers in a string with JavaScript

前端 未结 5 1687
谎友^
谎友^ 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条回答
  •  旧时难觅i
    2020-12-02 11:04

    Use Regular Expressions:

    var re = new RegExp(/^\d+/); //starts with digit, one or more
    var m = re.exec("4567 stuff is fun 67");
    alert(m[0]); //4567
    
    m = re.exec("stuff is fun 67");
    alert(m); // null
    

提交回复
热议问题