Regexp with comments


  • 0
    L
    var isNumber = function(s) {
        return /^\s*[+-]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)(e[+-]?[0-9]+)?\s*$/.test(s);
    };
    
    var _isNumber = function(s) {
        var rstr = '';
        
        // skip leading spaces
        rstr += '^\\s*';
        
        // match optional sign
        rstr += '[+-]?';
        
        // Two cases:
        rstr += '(';
        
        // 1) start with . followed by one or more digits
        rstr += '\\.[0-9]+';
        
        rstr += '|';
        
        // 2) start with one or more digits followed by optional decimals
        rstr += '[0-9]+(\\.[0-9]*)?';
        
        rstr += ')';
        
        // optional scientifc notation:
        rstr += '(e[+-]?[0-9]+)?';
        
        // skip trailing spaces
        rstr += '\\s*$';
        
        var r = new RegExp(rstr);
        return r.test(s);
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.