前端老生

JS 转义input输入的特殊字符

JS 转义input输入的特殊字符

 

将包含所有特殊字符的列表:[ ] { } ( ) \ ^ $ . | ? * + 等特殊符号, 增加反斜杠转义。

 

export const escapeRegExp = (text: string) => {
   return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
};