Nodejs String Escape escapeCharacters(chars)

Here you can find the source of escapeCharacters(chars)

Method Source Code

String.prototype.escapeCharacters = function(chars) {
    var foundChar = false;
    var length = chars.length;
    for (var i = 0; i < length; ++i) {
        if (this.indexOf(chars.charAt(i)) !== -1) {
            foundChar = true;//www . ja  va2 s .c o m
            break;
        }
    }

    if (!foundChar)
        return this;

    var result = "";
    for (var j = 0; j < this.length; ++j) {
        if (chars.indexOf(this.charAt(j)) !== -1)
            result += "\\";
        result += this.charAt(j);
    }

    return result;
};

Related

  1. escape()
    String.prototype.escape = function () {
        var tagsToReplace = {
            '&': '&amp;',
            '<': '&lt;',
            '>': '&gt;',
        };
        return this.replace(/[&<>]/g, function (tag) {
            return tagsToReplace[tag] || tag;
        });
    ...
    
  2. escape()
    String.prototype.escape = function() {
      var tagsToReplace = {
          '&': '&amp;',
          '<': '&lt;',
          '>': '&gt;'
      };
      return nl2br(this.replace(/[&<>]/g, function(tag) {
          return tagsToReplace[tag] || tag;
      }));
    ...
    
  3. escape()
    String.prototype.escape = function() {
      return escape(this.replace(/\s/g, '-'));
    
  4. 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");
    ...
    
  5. 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');
    ...
    
  6. escapeForRegExp()
    String.prototype.escapeForRegExp = function() {
        return this.escapeCharacters("^[]{}()\\.$*+?|");
    };
    
  7. escapeOnce()
    String.prototype.escapeOnce = function () {
      return this.replace(/"/g, '&quot;').replace(/>/g, '&gt;').replace(/</g, '&lt;').replace(/&(?!([a-zA-Z]+|#\d+);)/g, '&amp;');
    };
    
  8. escapeQuotes()
    String.prototype.escapeQuotes = function()
      var m = {"\"": "\\\"", "'": "\\'"};
      return String(this.replace("\\", "\\\\")).replace(/["']/g, function(s)
        return m[s];
      });
    
  9. escapeSecure()
    String.prototype.escapeSecure = function () {
      var returnStr, tmpStr;
      tmpStr = $(this);
      tmpStr.find("script").remove();
      returnStr = tmpStr.html();
      return returnStr;
    };