日期:2014-05-16  浏览次数:20362 次

谁来救我,replace的问题,真找到问题所在分不会少的~~~
以下是同一时间的调试结果

filePath "C:\Documents   and   Settings\GiL\Desktop\web\5400719.xml " String
filePath.replace( "\\ ", "\\\\ ") "C:\\Documents   and   Settings\GiL\Desktop\web\5400719.xml " String
filePath.replace( "/\\/g ", "\\\\ ") "C:\Documents   and   Settings\GiL\Desktop\web\5400719.xml " String
filePath.replace( "/\\/g ", "/ ") "C:\Documents   and   Settings\GiL\Desktop\web\5400719.xml " String
"C:\Documents   and   Settings\GiL\Desktop\web\5400719.xml ".replace( "\\ ", "\\\\ ") "C:Documents   and   SettingsGiLDesktopweb,00719.xml " String
"C:\Documents   and   Settings\GiL\Desktop\web\5400719.xml ".replace( "/\\/g ", "\\\\ ") "C:Documents   and   SettingsGiLDesktopweb,00719.xml " String

真是搞不懂是什么问题了,连Encode都改了,就是匹配有问题,我看网上的正则表达式都是这样写的,我的目的就是想把   \   换成   \\   而已,真晕吖~~~


------解决方案--------------------
replace(/\\/g, "\\\\ ")

第一个参数的引号去掉
------解决方案--------------------
正则的简单写法是: /正则内容/gi
正规写法是: new RegExp( "正则内容 ", "gi ");

gi都是可选的
------解决方案--------------------
这句话语法都通过不了
filePath.replace( "\\ ", "\\\\ ") "C:\\Documents and Settings\GiL\Desktop\web\5400719.xml "String
按楼上说的
------解决方案--------------------
如果你var filePath = "C:\\Documents and Settings\GiL\Desktop\web\5400719.xml ";这样弄的话,你alert一下(filePath)试试...
得出来的结果就是C:\Documents and SettingsGiLDesktopweb5400719.xml

这个时候\已经是当转义符了吧

------解决方案--------------------
你在前面定义string的时候\已经被当做转义符号了,所以你在后面再怎么转都是错的,你必须在内存中将\转化为\\,在页面上才能显示正常
------解决方案--------------------
补充一点:如果你是从DB里面读出来的,那么你可以在刚读出来的时候replace了,同样的,其他方式也一样,就是在刚刚得到这个值的时候就立即转换
------解决方案--------------------
alert( "C:\\Documents and Settings\GiL\Desktop\web\5400719.xml ".replace(/\\/g, "\\\\\\\\ "));