Encode and decode URL - Node.js String

Node.js examples for String:URL String

Description

Encode and decode URL

Demo Code


// from http://phpjs.org/functions/urlencode:573
util.urlencode = function(str) {
    str = (str + '').toString();
    return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}

// from http://phpjs.org/functions/urldecode:572
util.urldecode = function(str) {
  return decodeURIComponent((str + '').replace(/\+/g, '%20'));
}

Related Tutorials