https://leetcode.com/problems/valid-number/
Valid number
Validate if a given string is numeric.
Some Examples:
"0"
=true
" 0.1 "
=true
"abc"
=false
"1 a"
=false
"2e10"
=true
Note:it is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.
Regular technology which strong
1 /**2 * @param {string} s3 * @return {Boolean}4 */5 varIsnumber =function(s) {6s =S.trim ();7 if(S!== "" &&/^[-+]? (\d+\.?| \.\d+) \d* ([e| e][-+]?\d+)? $/. Test (s)) {8 return true;9 }Ten return false; One};
In the beginning, I did not think clearly, each one of the circumstances add an else, write a mess, and later found can be merged.
1 /*2 s = S.trim ();3 if (s = = = "") {4 return false;5 }else if (/^[-+]?[ e| E]/.test (s)) {6 return false;7 }else if (/^[-+]?0$/.test (s)) {8 return true;9 }else if (/^[-+]?[ 0-9]*$/.test (s)) {Ten return true; One }else if (/^[-+]?[ 0-9]+\. [0-9]*$/.test (s)) { A return true; - }else if (/^[-+]?[ 0-9]*\. [0-9]+$/.test (s)) { - return true; the }else if (/^[-+]?[ 0-9]*[e| E][-+]? [0-9]+$/.test (s)) { - return true; - }else if (/^[-+]? ( [0-9]+\. [0-9]*] [e| E][-+]? [0-9]+$/.test (s)) { - return true; + }else if (/^[-+]? ( [0-9]*\. [0-9]+] [e| E][-+]? [0-9]+$/.test (s)) { - return true; + } A return false; at */
[Leetcode] [JavaScript] Valid number