mirror of
https://github.com/zhigang1992/replace-in-file.git
synced 2026-04-29 05:05:33 +08:00
Add some regex usage instructions
This commit is contained in:
13
README.md
13
README.md
@@ -193,6 +193,19 @@ const options = {
|
||||
};
|
||||
```
|
||||
|
||||
### Custom regular expressions
|
||||
|
||||
Use the [RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) constructor to create any regular expression.
|
||||
|
||||
```js
|
||||
const str = 'foo';
|
||||
const regex = new RegExp('^' + str + 'bar', 'i');
|
||||
const options = {
|
||||
from: regex,
|
||||
to: 'bar',
|
||||
};
|
||||
```
|
||||
|
||||
### Using callbacks for `from`
|
||||
You can also specify a callback that returns a string or a regular expression. The callback receives the name of the file in which the replacement is being performed, thereby allowing the user to tailor the search string. The following example uses a callback to produce a search string dependent on the filename:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user