js中的replace方法和自定义replaceAll方法
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>String字符串测试</title>
</head>
<body>
<input type="submit" value="replace" onclick="replace()">
<input type="submit" value="replaceAll" onclick="replaceAll()">
<script type="text/javascript">
var testStr = "F:\\Develop\\tomcat\\apache-tomcat-7.0.73\\conf\\server.xml";
var value = "6#6#6#6#6#6#6";
function replace() {
alert("replace方法: " + testStr.replace("\\", "/"));
alert("replace方法: " + value.replace("#", "--"));
}
function replaceAll() {
// g,表示全部替换。
var reg = new RegExp("#", "g");
alert("replaceAll方法的结果: " + testStr.replace(/\\/g, "/"));
alert("replaceAll方法的结果: " + value.replace(reg, "--"));
}
</script>
</body>
</html>