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