-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregexps.ts
More file actions
27 lines (25 loc) · 1010 Bytes
/
regexps.ts
File metadata and controls
27 lines (25 loc) · 1010 Bytes
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
/**
* @fileoverview Regular expression utilities including escape-string-regexp implementation.
* Provides regex escaping and pattern matching helpers.
*/
// Inlined escape-string-regexp:
// https://socket.dev/npm/package/escape-string-regexp/overview/5.0.0
// MIT License
// Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
/**
* Escape special characters in a string for use in a regular expression.
*
* @example
* ```typescript
* escapeRegExp('foo.bar') // 'foo\\.bar'
* escapeRegExp('a+b*c?') // 'a\\+b\\*c\\?'
* new RegExp(escapeRegExp('[test]')) // /\[test\]/
* ```
*/
/*@__NO_SIDE_EFFECTS__*/
export function escapeRegExp(str: string): string {
// Escape characters with special meaning either inside or outside character sets.
// Use a simple backslash escape when it's always valid, and a `\xnn` escape when
// the simpler form would be disallowed by Unicode patterns' stricter grammar.
return str.replace(/[\\|{}()[\]^$+*?.]/g, '\\$&')
}