string-replace-enhanced allow you to go futher with the command replace for string in javascript. You have two options, use the object and implement his settings or use the functions in a standalone way.
Using npm:
npm install -S string-replace-enhanced
... and then:
import stringReplaceEnhanced from 'string-replace-enhanced'
// or
import {replaceBefore, replaceAfter, replaceAround, replace, replaceRegex} from 'string-replace-enhanced'
// or
const {default: stringReplaceEnhanced} = require('string-replace-enhanced')
In NodeJS:
const {default : stringReplaceEnhanced} = require('string-replace-enhanced')
// or
import {replaceBefore, replaceAfter, replaceAround, replace, replaceRegex} from 'string-replace-enhanced'
In a browser:
<script src="https://unpkg.com/string-replace-enhanced@latest/lib/sre.min.js"></script>
<script>
/* stringReplaceEnhanced is available here */
/* in object style */
new StringReplaceEnhanced.default(options) //constructor
/* standalone functions */
StringReplaceEnhanced.replace(...args) //function
</script>
There is two possibilities, use the object ou the separated functions.
With the object it will be possible to reuse a preset configuration or use the default one.
import stringReplaceEnhanced from 'string-replace-enhanced'
// If an option is missing, it will be given a default value
const options = {
after: ['«'],
around: [],
before: ['!', '?', ':', ';', '»'],
regex: [],
replacer: ' ',
toReplace: ' ',
};
// Create a new stringReplaceEnhanced object
const sre = new stringReplaceEnhanced(options);
// Use its methods simply giving the string to work on
console.log(sre.replaceBefore('Hello world !'));
// Hello world !
For the standalone functions you will need to specify every arguments
import { replaceBefore } from 'string-replace-enhanced'
console.log(replaceBefore('Hello world !', ' ', ' '))
// Hello world !
Check the links below to have the up to date version:
name | type | default | description |
---|---|---|---|
after | ReadonlyArray |
['«'] |
Array of char in need of potential need of replacement after itself |
around | ReadonlyArray |
[] |
Array of char in need of potential need of replacement around itself |
before | ReadonlyArray |
['!', '?', ':', ';', '»'] |
Array of char in need of potential need of replacement before itself |
regex | ReadonlyArray |
[] |
Array of regex, replace match by the replacer |
replacer | string | ' ' |
Replacer of matching toReplace |
toReplace | string | ' ' |
String to replace, in the function replace toReplace can be an array of string |
To see more about the methods and properties you can access to the documentation here
To see more about the functions or the configuration you can access it in the latest documentation here or one in the forked project:
npm i && npm run build:doc
The documentation will be available in the docs
file.
Generated using TypeDoc