Escaper
Escaper is a small JavaScript library to replace string literals, regular expressions, and comments from the JavaScript syntax.
Russian documentation
Supports:
' ... '
" ... "
` ... `
, ` ... ${...} `
/ ... /
// ...
, //* ...
, //! ...
, //# ...
, //@ ...
, //$ ...
/* ... */
, /** ... */
, /*! ... */
, /*# ... */
, /*@ ... */
, /*$ ... */
Install
https://raw.githubusercontent.com/kobezzza/Escaper/master/dist/escaper.min.js
or
npm install escaper
or
bower install escaper
or
git clone https://github.com/kobezzza/Escaper
Usage
const
str = 'Hello "world" and \'friends\'',
content = [];
Escaper.replace(str, content)
Escaper.replace(str, ["'"])
Escaper.replace(str, -1)
Escaper.replace(str, {"'": -1})
Escaper.replace(str, {strings: false})
Escaper.replace(str, {strings: ["'"]})
Escaper.replace(str, {strings: {"'": -1}})
Escaper.paste(str, content);
API
Escaper.replace(str, how?, store?): string
Replaces all found blocks ' ... '
, " ... "
, ` ... `
, / ... /
, // ...
, /* ... */
to
escape blocks from the specified string and returns a new string.
Arguments
string
str
— source string;string[] | Record<string, string[] | Record<string, boolean | -1> | false | -1> | false | -1
how?
— parameters:
Possible values
If a value is set to -1
, then all found matches will be removed from the final string.
If the value is set to boolean
it will be replaced/passed.
'label'
'singleComments'
'multComments'
'comments'
'strings'
'literals'
"'"
'"'
'`'
'/'
'//'
'//*'
'//!'
'//#'
'//@'
'//$'
'/*'
'/**'
'/*!'
'/*#'
'/*@'
'/*$'
The parameters can be specified as an array (escapes only explicitly specified sequences)
or like an object (disables/excludes by a literal or group). Also, if you set the parameter value as -1
,
then all found sequences will be removed from the string.
string[]
store = Escaper.content
— store for matches.
Escaper.paste(str, store?, rgxp?): string
Replaces all found escape blocks to real content from the specified string and returns a new string.
Arguments
string
str
— source string;string[]
store = Escaper.content
— store of matches;RegExp
rgxp?
— RegExp to search, e.g. /__ESCAPER_QUOT__(\d+)_/g
.
The MIT License.