All files / libs/lang escapeRegExp.js

100% Statements 6/6
100% Branches 0/0
100% Functions 1/1
100% Lines 6/6
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                1x 1x                         1x 47x                   1x                 1x  
/**
 * @module      libs/lang/escapeRegExp
 * @createdAt   2016-06-30
 *
 * @copyright   Copyright (c) 2016 Zhonglei Qiu
 * @license     Licensed under the MIT license.
 */
 
var gre = /[-.*+?^${}()|[\]/\\]/g
var re = /[-.*+?^${}()|[\]/\\]/
 
/**
 * 对字符串中的字符进行转义
 *
 * 在用 new RegExp 创建正则表达式时很常用
 *
 * @param  {String} str 要转义的字符串
 * @return {String}     转义后的字符串
 *
 * @author    Zhongle Qiu
 * @since     2.0.0
 */
module.exports = function(str) {
  return str.replace(gre, '\\$&')
}
 
/**
 * 转义使用的全局的正则表达式
 * @type {RegExp}
 *
 * @author    Zhongle Qiu
 * @since     2.0.0
 */
module.exports.gre = gre
 
/**
 * 转义使用的非全局的正则表达式
 * @type {RegExp}
 *
 * @author    Zhongle Qiu
 * @since     2.0.0
 */
module.exports.re = re