35 lines
1.0 KiB
JavaScript
35 lines
1.0 KiB
JavaScript
'use strict'; /**
|
|
* Copyright (c) 2014, Facebook, Inc. All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*
|
|
*
|
|
*/
|
|
|
|
const path = require('path');
|
|
|
|
const escapePathForRegex = dir => {
|
|
if (path.sep === '\\') {
|
|
// Replace "\" with "/" so it's not escaped by escapeStrForRegex.
|
|
// replacePathSepForRegex will convert it back.
|
|
dir = dir.replace(/\\/g, '/');
|
|
}
|
|
return replacePathSepForRegex(escapeStrForRegex(dir));
|
|
};
|
|
|
|
const escapeStrForRegex = string =>
|
|
string.replace(/[[\]{}()*+?.\\^$|]/g, '\\$&');
|
|
|
|
const replacePathSepForRegex = string => {
|
|
if (path.sep === '\\') {
|
|
return string.replace(/(\/|\\(?!\.))/g, '\\\\');
|
|
}
|
|
return string;
|
|
};
|
|
|
|
module.exports = {
|
|
escapePathForRegex,
|
|
escapeStrForRegex,
|
|
replacePathSepForRegex }; |