不太懂js的字符串处理,请指教,在线等.
日期:2007-03-31 荐:
不太懂js的字符串处理,请指教,在线等.打个比方吧已知filename="D:\Program Files\BitComet\rules\tracker.dat"如何处理才能使结果filename="tracker.dat"请大家指点一下.<script>var str1="D:\\Program Files\\BitComet\rules\\tracker.dat";var str2=str1.substr(str1.lastIndexOf("\\") 1);alert(str1 "" str2);</script>用正则也可以<script>var str1="D:\\Program Files\\BitComet\rules\\tracker.dat";var str2=str1.substr(str1.lastIndexOf("\\") 1);/([^\\]\w \.\w $)/g.test(str1);var str3=RegExp.$1;alert(str1 "" str2 "" str3);</script>filename="d:\Program Files\BitComet\rules\tracker.dat";你的原字符串无法匹配,因为处理器将\解释为一个转义符号\p=p,\b=b,\r\t为非显示字符。如果你要匹配\则需要在字符串里写入两个\\你可以输出你的字符串看看效果:filename1="D:\Program Files\BitComet\rules\tracker.dat"filename2="D:\\Program Files\\BitComet\rules\\tracker.dat";alert(filename1);alert(filename2);如果不想转换那就用VBS,如果这些正好是asp中的话那刚好就用VBS吧<script language=vbscript>str1="D:\Program Files\BitComet\rules\tracker.dat"set re=new RegExpre.Pattern="([^\\]\w \.\w $)"re.Global=trueset ss=re.Execute(str1)str3=ss(0)str2=mid(str1,InStrRev(str1,"\") 1)msgbox str1&chr(13)&chr(10)&str2&chr(13)&chr(10)&str3</script>试试看:<input type=text value="D:\Program Files\BitComet\rules\tracker.dat" name="filepath" onblur="document.all.www.value=value.replace(/\\/g,'\\\\');alert(getfilename(document.all.www.value));"><input type=text name="www"><script>function getfilename(str){var s=str.split("\\\\");return s[s.length-1];}</script><input type=text value="D:\Program Files\BitComet\rules\tracker.dat" name="filepath"> <script>function getfilename(str){var s=str.split("\\\\");return s[s.length-1];}alert(getfilename(document.all.filepath.value.replace(/\\/g,'\\\\')));</script>function getFileName(fname){ var filename=fname.split("\\"); return(filename[filename.length-1]);}<script>var str1="D:\\Program Files\\BitComet\rules\\tracker.dat";var str2=str1.substr(str1.lastIndexOf("\\") 1);alert(str1 "" str2);</script>
标签: