Javascript 字符串替换

时间:2023-12-11 08:42:14

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<meta charset="utf-8" />
</head>
<body>
<script language="javascript">
var r = "1Null2Null3Null";
//将字母Null置空
alert(r.replace("Null", ""));

//结果:12Null3Null
</script>

<script language="javascript">
var r = "1Null2Null3Null";
//将字母Null置空
alert(r.replace(new RegExp("Null", 'g'), ""));

//结果:123; replace 的第一个参数可以是正则表达式,/g标识全文匹配。
</script>
</body>
</html>