<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標題文檔</title>
</head>
<body>
<input id="text1" name="text1" value="D:/MyDesktop/ruby/baby/ok">
</body>
<script language="javascript">
var str=document.getElementById("text1").value;
alert(str);
str=str.replace(////g,'////');
alert(str);
</script>
</html>
以上實例源代碼,這裏有有兩處關鍵點
:
1。replace 替換全部匹配字符 replace(s1,s2),默認情況下,替換第一處匹配的字符,要是替換全部則 用模式中的子表達式 例:/模式子表達式/g ,這裏的g就可以替換多處。
2.將“/” 替換爲:“//” 其實表示"/"很簡單關鍵是,表示"//",javascript中特殊字符是要進行轉義的所以"/" -對應-"//" ,"//" -對應- “////”,