JS-替换全部全部字符串

  替换字符串中的某些子串,通常我们会使用sInput.replace(sA,sB)的方法,但是这个方法只会把sInput中的第一个sA替换成sB,那么假如我们要把sInput中的所有sA替换成sB,这个方法就不满足我们的要求了。

举例子说明:

  只能替换第一个匹配的字符串:

 1 <html>
 2     <title></title>
 3     <head>
 4         <script type="text/javascript">
 5             
 6             function myReplaceOne(sInput, sChar, sReplaceChar) {
 7                 if(sInput==""||sInput==undefined) {
 8                     return "";
 9                 }
10 
11                 return sInput.replace(sChar, sReplaceChar);
12             }
13             
14             
15 
16 
17             function window.onload(){
18                 var sInput="这里是要替换的字符串的例子,在这个例子中,可以把要替换的子串都替换掉。";
19                 document.write("原始字符串:" + sInput);
20                 document.write("<p/>");
21                 document.write("第一个子串被替换后的字符串:" + myReplaceOne(sInput,"例子","测试"));
22                 document.write("<p/>");
23                 
24 
25                 
26                 
27             }
28         </script>
29     </head>
30 
31 <body>
32     
33 </body>
34 </html>
View Code

截图:

 

具体的JS实现方法代码如下:

 1 /*
 2  * 功    能:替换字符串中某些字符(只能是第一个被替换掉)
 3  * 参    数:sInput-原始字符串  sChar-要被替换的子串 sReplaceChar-被替换的新串
 4  * 返 回 值:被替换后的字符串
 5  * 创 建 人:maojw
 6  * 创建时间:2013-08-27
 7  *
 8  */
 9 function myReplaceOne(sInput, sChar, sReplaceChar) {
10     if (sInput == "" || sInput == undefined) {
11         return "";
12     }
13 
14     return sInput.replace(sChar, sReplaceChar);
15 }

 

  如果想替换所有的子串,其实,正则表达式很容易就实现了这个需求,使用sInput.replace(/sA/g,sB)的方法就可以了。

所有匹配的子串都会被替换掉:

 1 <html>
 2     <title></title>
 3     <head>
 4         <script type="text/javascript">
 5             
 6             function myReplaceOne(sInput, sChar, sReplaceChar) {
 7                 if(sInput==""||sInput==undefined) {
 8                     return "";
 9                 }
10 
11                 return sInput.replace(sChar, sReplaceChar);
12             }
13             
14             function myReplace(sInput, sChar, sReplaceChar) {
15 
16                 if(sInput==""||sInput==undefined) {
17                     return "";
18                 }
19 
20                 var oReg =new RegExp(sChar,"g");
21 
22                 return sInput.replace(oReg, sReplaceChar);
23 
24             }
25             
26             
27 
28 
29             function window.onload(){
30                 var sInput="这里是要替换的字符串的例子,在这个例子中,可以把要替换的子串都替换掉。";
31                 document.write("原始字符串:" + sInput);
32                 document.write("<p/>");
33                 document.write("第一个子串被替换后的字符串:" + myReplaceOne(sInput,"例子","测试"));
34                 document.write("<p/>");
35                 document.write("所有的子串被替换后的字符串:"+myReplace(sInput,"例子","测试"));
36                 document.write("<p/>");
37                 
38             }
39         </script>
40     </head>
41 
42 <body>
43     
44 </body>
45 </html>
View Code

 

截图如下:

 具体的js函数代码如下:

/*
 * 功    能:替换字符串中某些字符
 * 参    数:sInput-原始字符串  sChar-要被替换的子串 sReplaceChar-被替换的新串
 * 返 回 值:被替换后的字符串
 * 创 建 人:maojw
 * 创建时间:2013-08-27
 *
 */
function myReplace(sInput, sChar, sReplaceChar) {

    if(sInput==""||sInput==undefined) {
        return "";
    }

    var oReg =new RegExp(sChar,"g");

    return sInput.replace(oReg, sReplaceChar);

}

 

如果想实现高亮显示文本的话,也不难,就是把替换的文本加上html标签就可以了。

 1 <html>
 2     <title></title>
 3     <head>
 4         <script type="text/javascript">
 5             
 6             function myReplaceOne(sInput, sChar, sReplaceChar) {
 7                 if(sInput==""||sInput==undefined) {
 8                     return "";
 9                 }
10 
11                 return sInput.replace(sChar, sReplaceChar);
12             }
13             
14             function myReplace(sInput, sChar, sReplaceChar) {
15 
16                 if(sInput==""||sInput==undefined) {
17                     return "";
18                 }
19 
20                 var oReg =new RegExp(sChar,"g");
21 
22                 return sInput.replace(oReg, sReplaceChar);
23 
24             }
25             
26             
27 
28 
29             function window.onload(){
30                 var sInput="这里是要替换的字符串的例子,在这个例子中,可以把要替换的子串都替换掉。";
31                 document.write("原始字符串:" + sInput);
32                 document.write("<p/>");
33                 document.write("第一个子串被替换后的字符串:" + myReplaceOne(sInput,"例子","测试"));
34                 document.write("<p/>");
35                 document.write("所有的子串被替换后的字符串:"+myReplace(sInput,"例子","测试"));
36                 document.write("<p/>");
37                 document.write("所有的子串被替换后的字符串,并且高亮显示:"+myReplace(sInput,"例子","<font color='red'>测试</font>"));
38                 document.write("<p/>");
39                 document.write("所有的子串被替换后的字符串,并且高亮显示子串:"+myReplace(sInput,"(例子)","<font color='red'>$1</font>"));
40                 document.write("<p/>");
41                 
42                 
43                 
44             }
45         </script>
46     </head>
47 
48 <body>
49     
50 </body>
51 </html>
View Code

 

截图如下:

 

 

posted @ 2013-08-27 23:23  毛毛大王  阅读(10557)  评论(0编辑  收藏  举报