简体   繁体   中英

Type Ahead Regex For String Of Numbers

I am using typeahead to perform a server side query to look up a phone number associated with a Meteor user. However I can't get the Regular Expression Correct. What should my RegExp look like? The phoneNumber field should look like : 1234567890 , ie 10 numbers.

   searchPhone: function(query, options) {
    options = options || {};

    // guard against client-side DOS: hard limit to 50
    if (options.limit) {
        options.limit = Math.min(50, Math.abs(options.limit));
    } else {
        options.limit = 50;
    }

    var regex = new RegExp("^" + query);
    return Meteor.users.find({'profile.phoneNumber': {$regex:  regex}}, options).fetch();
}

You want a regex that only allows digits [0-9] and exactly 10 of them: {10}

var regex = /([0-9]){10}/;

RegExr is awesome for practicing your regex-fu.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM