string replace tests

Basic formating

by toubia95

CSS

/* http://jacwright.com/projects/javascript/date_format/ */

/* http://stackoverflow.com/questions/18985201/javascript-replace-with-variable-in-string-with-regular-expression */

/* classes : 
http://www.pompage.net/traduction/classe-et-heritage-en-javascript
http://ejohn.org/blog/simple-javascript-inheritance/
*/

JavaScript

function cgf(d, format) {
    var map = {
        "Y": d.getFullYear(),
            "m": d.getMonth() + 1,
            "d": d.getDate()
    };
    return format.replace(/{(\w+)}/g, function (match, p) {
        return map.hasOwnProperty(p) ? map[p] : '';
    });
}

var d = new Date();

console.log(cgf(d,"Aujourd'hui, nous sommes le {d}/{m}/{Y} !"));