string .replace(/a/g, '!') .replace(/b/g, '#') .replace(/c/g, '没规律的') .replace(/d/g, '没规律的') .replace(/e/g, '没规律的') // 以下省略20行 .....
以上,像这种字符串替换,源文本很大、高频率调用的情况下
如何写怎么省内存和提高性能呢?
let mapReplace = { 'a' : '!', 'b' : '#', 'c' : '没规律的', 'd' : '没规律的', }; <string>.replace(/a|b|c|d/g, function($match){ if($match in mapReplace === true) return mapReplace[$match]; return $match; })
2.1m questions
2.1m answers
63 comments
56.7k users