Monday, 3 June 2013

mongo db RegExp

var regex = new RegExp("^mongo(?:db)?://(?:|([^@/]*)@)([^@/]*)(?:|/([^?]*)(?:|\\?([^?]*)))$");
var match = process.env.MONGOHQ_URL.match(regex);
var auth = match[1].split(':', 2);
auth[0] = decodeURIComponent(auth[0]);
auth[1] = decodeURIComponent(auth[1]);
db.authenticate(auth[0], auth[1], function(err, success) {
if (err) {
console.error(err);
} else {
// worked >:O
}
});
//OR
var regex = new RegExp("^mongo(?:db)?://(?:|([^@/]*)@)([^@/]*)(?:|/([^?]*)(?:|\\?([^?]*)))$");
var con_url = "mongodb://heroku_app160846:2n2tlv5la7mi5sgpbhvj9j@ds027908.mongolab.com:27908/heroku_app161146";
var match = con_url.match(regex);
var auth = match[1].split(':', 2);
auth[0] = decodeURIComponent(auth[0]);
auth[1] = decodeURIComponent(auth[1]);
console.log(auth)
view raw mongo db RegExp hosted with ❤ by GitHub

No comments:

Post a Comment