https://github.com/lamansky/char-escape
[Node.js] Finds all instances of given characters in a string and inserts an escape sequence before each one.
https://github.com/lamansky/char-escape
Last synced: 3 months ago
JSON representation
[Node.js] Finds all instances of given characters in a string and inserts an escape sequence before each one.
- Host: GitHub
- URL: https://github.com/lamansky/char-escape
- Owner: lamansky
- License: mit
- Created: 2019-09-02T09:38:41.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-09-02T09:40:53.000Z (almost 7 years ago)
- Last Synced: 2025-08-09T03:06:09.428Z (11 months ago)
- Language: JavaScript
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license.txt
Awesome Lists containing this project
README
# char-escape
Finds all instances of given characters in a string and inserts an escape sequence before each one.
## Installation
Requires [Node.js](https://nodejs.org/) 8 or above.
```bash
npm i char-escape
```
## API
The module exports a single function.
### Parameters
1. `chars` (string or array of strings): The characters that should be escaped. This can either be a string of characters, or an array of characters/strings.
2. Optional: `escapeChar` (string): The character/string that should be placed before every character/string in `chars` and before all instances of `escapeChar` that are already in the string. Defaults to `\`.
### Return Value
Returns a function which accepts a single parameter: a string to which the escape procedure should be applied.
## Example
```javascript
const charEscape = require('char-escape')
// Escape characters listed in a string
charEscape('{}[]')('[This is] a {test}') // \[This is\] a \{test\}
// Escape strings listed in an array
charEscape(['is', 'a'])('This is a test') // This \is \a test
// Custom escape character
charEscape('{}[]', '@')('[This is] a {test}') // @[This is@] a @{test@}
```
## Related
* [char-unescape](https://github.com/lamansky/char-unescape)