Topic describes

Their thinking

  • In this case, the regular expression solution is used as a practice of your own regular expression
  1. Trim removes whitespace from both sides of the string.
  2. The decimal case.
  3. Integer case.
  4. Is e or e, followed by an integer

The above four cases are combined to determine whether a string representing a value matches. The problem is really clear.

The problem solving code

var isNumber = function(s) {
    // Remove whitespace from both sides of the string
    s = s.trim();
    // Check if there is a + - sign, continue to check if there is
    const num = s.match(/ ^ (+ -)? \d{1,}\.([eE][+-]? \d+)? $/);
    const num2 = s.match(/ ^ (+ -)? \d{1,}\.\d{1,}([eE][+-]? \d+)? $/);
    const num3 = s.match(/ ^ (+ -)? \.\d{1,}([eE][+-]? \d+)? $/);
    const num4 = s.match(/ ^ (+ -)? \d+([eE][+-]? \d+)? $/)
    if(! num && ! num2 && ! num3 && ! num4) {return false;
    } else {
        return true}};Copy the code

Conclusion (this topic gives us the enlightenment of thinking)

  • Learn how to use regular expressions flexibly.