js不提供replaceAll方法,要用正规表达式实现。
xhtml代码
- <!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>
- <title> new document </title>
- <meta name="generator" content="editplus" />
- <meta name="author" content="" />
- <meta name="keywords" content="" />
- <meta name="description" content="" />
- </head>
-
- <body>
- <script type="text/javascript">
- <!--
- alert("aSDFaSDF".replace("a","A"));
- alert("127.0.0.1".replace("\.",""));
-
- String.prototype.replaceAll = function(s1,s2){
- return this.replace(new RegExp(s1,"gm"),s2);//g=global, m=multiLine
- }
- alert("aSDFaSDF".replaceAll("a","A"));
- alert("127.0.0.1".replaceAll("\\.",""));
-
- //或者
- alert("aSDFaSDF".replace(/a/g,"A"));
- alert("127.0.0.1".replace(/\./g,""));
- //-->
- </script>
- </body>
- </html>