Javascript String Prototype Encode RegExp Symbols

Description

Javascript String Prototype Encode RegExp Symbols


String.prototype.encodeRegExp = function() {
  return this.replace(/([*+-.?^${}()|[\]\/\\])/g, '\\$1');
}

console.log( "[] + ? + ^ { $ }".encodeRegExp() );



PreviousNext

Related