-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
59 lines (53 loc) · 2.24 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* @file Exports a simple function that will escape a string literal for use as an argument in the standard JS RegExp
* constructor.
* @module escape-regex-string
* @author monotonee <[email protected]>
* @license MIT
*/
'use strict';
// This is temporarily declared with "var" instead of "const" to maintain the absolute broadest compatibility.
var defaultEscapeCharsRegex = /[-|\\{}()[\]^$+*?.]/g;
/**
* Escape a string literal for use as an argument in the standard RegExp constructor.
* @alias module:escape-regex-string
* @param {string} unescapedString - The string containing a regex pattern to be escaped.
* @param {RegExp} [escapeCharsRegex] - An optional RegEx pattern containing a set of characters to escape. If not
* passed, value will be set to default.
* @return {string} - The escaped regex pattern string.
* @throws {TypeError} - Arguments must be correct type.
* @property {RegExp} defaultEscapeCharsRegex - A read-only property that contains the default escape character RegExp
* instance. The value of this property is the value used when the optional second argument is omitted in a call to
* {@link module:escape-regex-string}.
*/
function escapeRegexString(unescapedString, escapeCharsRegex) {
// Validate arguments.
if (Object.prototype.toString.call(unescapedString) !== '[object String]') {
throw new TypeError('Argument 1 should be a string.');
}
if (escapeCharsRegex === undefined) {
escapeCharsRegex = defaultEscapeCharsRegex;
} else if (Object.prototype.toString.call(escapeCharsRegex) !== '[object RegExp]') {
throw new TypeError('Argument 2 should be a RegExp object.');
}
// Escape the string.
return unescapedString.replace(escapeCharsRegex, '\\$&');
}
/***
* JSDoc STILL fails to parse this properly due to the use of Object.defineProperty. It has been documented as a
* property of escapeRegexString. Note the three asterisks at the beginning of this comment to disable JSDoc parsing.
* @readonly
* @static
* @type {RegExp}
*/
Object.defineProperty(
escapeRegexString,
'defaultEscapeCharsRegex',
{
configurable: false,
enumerable: true,
value: defaultEscapeCharsRegex,
writable: false
}
);
module.exports = escapeRegexString;