JS Traditional way to take the numbers

Source: Internet
Author: User

Why use the regular?! I am not very understanding at the beginning, I am too lazy to learn, but look at the following if else estimates that you are enough, although it is difficult to learn, it is difficult to remember that the operation should be similar to the use of regular to do. Below I use the traditional string manipulation to do a function of taking numbers.

//the task of the function findnum is to find the string "1212 21 009 090 00" and print it outvarStr= "1212dasdad21jif009fajf090adv00";functionFindNum (str) {varArr=[];//empty arrays store variables for a while;     for(vari=0;i<str.length;i++){        //iterate through each bit of a string        varOstr=Str.charat (i); //taking 0-9 out of range is not a string        if(ostr<= ' 9 ' &&ostr>= ' 0 '){            //Array Push method AddArr.push (OSTR)}} //return Results    returnarr;} Console.log (FindNum (str));/*The results are printed as ["1", "2", "1", "2", "2", "1", "0", "0", "9", "0", "9", "0", "0", "0"] and you can see that this is not what we want.*/

After modifying a function

//Add a new variable tmpvarStr= "1212dasdad21jif009fajf090adv00";functionFindNum (str) {varArr=[]; varTmp= ";  for(vari=0;i<str.length;i++){                varOstr=Str.charat (i); if(ostr<= ' 9 ' &&ostr>= ' 0 '){            //is the number to perform the bottom operationtmp+=Ostr; }Else{            //non-numeric perform the following actions            if(TMP) {Arr.push (TMP); TMP= ";//empties the variable, used for traversing the following string;            }        }                }    //return Results    returnarr;} Console.log (FindNum (str));/*The results are ["1212", "21", "009", "090"] now much better, but the result we are looking for should be ["1212", "21", "009", "090", "00"] now found is ["1212", "21", "009 "," 090 "] and one less.*/

Modify the function again:

//increase the If else judgment again after the loop endsvarStr= "1212dasdad21jif009fajf090adv00";functionFindNum (str) {varArr=[]; varTmp= ";  for(vari=0;i<str.length;i++){                varOstr=Str.charat (i); if(ostr<= ' 9 ' &&ostr>= ' 0 '){            //is the number to perform the bottom operationtmp+=Ostr; }Else{            //non-numeric perform the following actions            if(TMP) {Arr.push (TMP); TMP= ";//empties the variable, used for traversing the following string;            }        }                        }    //after the end of the loop is judged again, because the end is a number, the end of the loop will only walk is the number of operations, and added to the array of this action will have no chance to go.     if(TMP) {Arr.push (TMP); TMP= ";//The last time the variable is emptied    }    //return Results    returnarr;} Console.log (FindNum (str));/*The results are printed as ["1212", "21", "009", "090", "00"] and now we want the result.*/

Over the past 2 times the toss finally reached the goal we want to find the function of the number is OK, if the complex points of the match, once added a push if else, think is enough! I began to learn the regular, no longer lazy (⊙o⊙) ...

JS Traditional way to take the numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.