编程语言
首页 > 编程语言> > 冷知识 javascript 对特殊符号的转义顺序

冷知识 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, "\\|");

标签:javascript,replace,斜杠,searchSegment,转义,特殊符号
来源: https://www.cnblogs.com/tangge/p/16594107.html