Skip to content

Commit 93999af

Browse files
committed
feat: export replace.createSearchRegExp function
1 parent 25f023e commit 93999af

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,9 @@ a `RegExp` without the `g` (global) flag.
6565
## Acknowledgements
6666

6767
Thanks to Nathan Bubna for his [`case`](https://github.com/nbubna/Case) package, which powers `preserve-case`!
68+
69+
### `replace.createSearchRegExp(query, [options])`
70+
71+
Creates a `RegExp` that matches for any case of the given `query` string.
72+
Like the other functions, `options.caseTypes` defaults to all types built into
73+
the [`case`](https://github.com/nbubna/Case) package.

lib/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,17 @@ function doReplace(str, query, replacement) {
3939
}
4040

4141
function replace(str, query, replacement, options) {
42-
var caseTypes = options && options.caseTypes || Case._.types
4342
if (typeof query === 'string') {
44-
query = new RegExp(caseTypes.map(function (type) { return Case[type](query) }).join('|'))
43+
query = replace.createSearchRegExp(query, options)
4544
}
4645
return doReplace(str, query, replacement)
4746
}
4847

48+
replace.createSearchRegExp = function createSearchRegExp(query, options) {
49+
var caseTypes = options && options.caseTypes || Case._.types
50+
return new RegExp(caseTypes.map(function (type) { return Case[type](query) }).join('|'))
51+
}
52+
4953
replace.all = function replaceAll(str, query, replacement, options) {
5054
var caseTypes = options && options.caseTypes || Case._.types
5155
if (query instanceof RegExp) {

0 commit comments

Comments
 (0)