js替换字符串的所有示例代码

复制代码 代码如下:

  /**

  * 替换字符串中所有

  * @param obj 原字符串

  * @param str1 替换规则

  * @param str2 替换成什么

  * @return 替换后的字符串

  */

  function replaceAll(obj,str1,str2){

  var result = obj.replace(eval("/"+str1+"/gi"),str2);

  return result;

  }

  例如:

  

复制代码 代码如下:

  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

  <html>

  <head>

  <title> New Document </title>

  <meta name="Generator" content="EditPlus">

  <meta name="Author" content="">

  <meta name="Keywords" content="">

  <meta name="Description" content="">

  <script type="text/javascript">

  function replaceAll(obj,str1,str2){

  var result = obj.replace(eval("/"+str1+"/gi"),str2);

  return result;

  }

  alert(replaceAll("111,222,333",",","|"));

  </script>

  </head>

  <body>

  </body>

  </html>