冷知识 javascript 对特殊符号的转义顺序

            //反斜杠转移写在最前面,这样就不会影响后面的转换
            searchSegment = searchSegment.replace(/\\/g, "\\\\"); 
            searchSegment = searchSegment.replace(/\+/g, "\\+").replace(/\-/g, "\\-").replace(/\?/g, "\\?").replace(/\*/g, "\\*").replace(/\./g, "\\.");
            searchSegment = searchSegment.replace(/{/g, "\\{").replace(/}/g, "\\}").replace(/\(/g, "\\(").replace(/\)/g, "\\)").replace(/\[/g, "\\[").replace(/\]/g, "\\]");
            searchSegment = searchSegment.replace(/\$/g, "\\$").replace(/\^/g, "\\^").replace(/\//g, "\/").replace(/\|/g, "\\|");
posted @ 2022-08-17 10:23  【唐】三三  阅读(59)  评论(0编辑  收藏  举报