Nodejs String Escape escape()

Here you can find the source of escape()

Method Source Code

String.prototype.escape = function () {
    var tagsToReplace = {
        '&': '&',
        '<': '&lt;',
        '>': '&gt;',
    };//from   www .j  ava  2s.  co m
    return this.replace(/[&<>]/g, function (tag) {
        return tagsToReplace[tag] || tag;
    });
};

function jsObj2phpObj(object) {
    var json = "{";
    for (property in object) {
        var value = object[property];
        if (typeof (value) == "string") {
            json += '"' + property + '":"' + value + '",'
        } else {
            if (!value[0]) {
                json += '"' + property + '":' + value + ',';
            } else {
                json += '"' + property + '":[';
                for (prop in value)
                    json += '"' + value[prop] + '",';
                json = json.substr(0, json.length - 1) + "],";
            }
        }
    }
    return json.substr(0, json.length - 1) + "}";
}

Related

  1. escape()
    String.prototype.escape = function () {
        var entityMap = {
            "&": "&amp;",
            "<": "&lt;",
            ">": "&gt;",
            '"': '&quot;',
            "'": '&#39;'
        };
        return this.replace(/[&<>"']/g, function (s) {
    ...
    
  2. escape()
    String.prototype.escape = function() {
        var tagsToReplace = {
            '&': '&amp;',
            '<': '&lt;',
            '>': '&gt;'
        };
        return this.replace(/[&<>]/g, function(tag) {
            return tagsToReplace[tag] || tag;
        });
    ...
    
  3. escape()
    String.prototype.escape = function () {
        var tagsToReplace = {
            '&': '&amp;',
            '<': '&lt;',
            '>': '&gt;'
        };
        return this.replace(/[&<>]/g, function (tag) {
            return tagsToReplace[tag] || tag;
        });
    ...
    
  4. escape()
    String.prototype.escape = function () {
        var entityMap = {
            '&': '&amp;',
            '<': '&lt;',
            '>': '&gt;',
            '"': '&quot;',
            "'": '&#39;',
            '/': '&#x2F;'
        };
    ...
    
  5. escape()
    String.prototype.escape = function() {
      return this
        .replace(/&(?!\w+;)/g, '&amp;')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;')
        .replace(/"/g, '&quot;');
    };
    
  6. escape()
    String.prototype.escape = function() {
      var tagsToReplace = {
          '&': '&amp;',
          '<': '&lt;',
          '>': '&gt;'
      };
      return nl2br(this.replace(/[&<>]/g, function(tag) {
          return tagsToReplace[tag] || tag;
      }));
    ...
    
  7. escape()
    String.prototype.escape = function() {
      return escape(this.replace(/\s/g, '-'));
    
  8. escapeSpecialChars()
    String.prototype.escapeSpecialChars = function() {
        return this.replace(/\\n/g, "\\n")
                   .replace(/\\'/g, "\\'")
                   .replace(/\\"/g, '\\"')
                   .replace(/\\&/g, "\\&")
                   .replace(/\\r/g, "\\r")
                   .replace(/\\t/g, "\\t")
                   .replace(/\\b/g, "\\b")
                   .replace(/\\f/g, "\\f");
    ...
    
  9. escapeSpecialChars()
    String.prototype.escapeSpecialChars = function () {
      return this.replace(/\\n/g, '\\\\n')
      .replace(/\\'/g, '\\\'')
      .replace(/\\'/g, '\\\'')
      .replace(/\\&/g, '\\\&')
      .replace(/\\r/g, '\\\r')
      .replace(/\\t/g, '\\\\t')
      .replace(/\\b/g, '\\\b')
      .replace(/\\f/g, '\\\f');
    ...