1.自http://jorkin.reallydo.com/article.asp?id=275
第一次发现JavaScript中replace() 方法如果直接用str.replace("-","!") 只会替换第一个匹配的字符. 而str.replace(//-/g,"!")则可以全部替换掉匹配的字符(g为全局标志)。 replace() The replace() method returns the string that results when you replace text matching its first argument (a regular expression) with the text of the second argument (a string). If the g (global) flag is not set in the regular expression declaration, this method replaces only the first occurrence of the pattern. For example, var s = "Hello. Regexps are fun.";s = s.replace(//./, "!"); // replace first period with an exclamation pointalert(s); produces the string “Hello! Regexps are fun.” Including the g flag will cause the interpreter to perform a global replace, finding and replacing every matching substring. For example, var s = "Hello. Regexps are fun.";s = s.replace(//./g, "!"); // replace all periods with exclamation pointsalert(s); yields this result: “Hello! Regexps are fun!” 所以可以用以下几种方式.: string.replace(/reallyDo/g, replaceWith); string.replace(new RegExp(reallyDo, 'g'), replaceWith); string:字符串表达式包含要替代的子字符串。 reallyDo:被搜索的子字符串。 replaceWith:用于替换的子字符串。 <mce:script type="text/javascript"><!-- String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) { if (!RegExp.prototype.isPrototypeOf(reallyDo)) { return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi": "g")), replaceWith); } else { return this.replace(reallyDo, replaceWith); } } // --></mce:script> 本文来源于 KinJAVA日志 (http://jorkin.reallydo.com) 原文地址: http://jorkin.reallydo.com/article.asp?id=275
2.自http://yueguangyuan.javaeye.com/blog/31744
方法: string.replace(new RegExp(oldString,"gm"),newString))
gm g=global, m=multiLine , 大致上方法就是这样的,可以实现替换全部指定字串
另一个简单的验证JS的方法:
在浏览器地址栏输入
javascript:alert("abcabcabc".replace(new RegExp("a","gm"),"ad"))
这样比较省事 ;) ,不知道多行的会不会很方便
3.自http://www.yaosansi.com/post/304.html
方法一:
个人认为最好的方法.采用的是正则表达式,这是最核心的原理.
其次.这个方法使用了JavaScript 的prototype 属性
其实你不使用这个属性一样可以用函数实现.但这样做后用起来比较方便.
下面就来看看这个属性是怎么来用的.
返回对象类型原型的引用。
objectName.prototype
objectName 参数是对象的名称。
说明
用 prototype 属性提供对象的类的一组基本功能。对象的新实例“继承”赋予该对象原型的操作。
例如,要为 Array 对象添加返回数组中最大元素值的方法。要完成这一点,声明该函数,将它加入 Array.prototype,并使用它。
function array_max( ){
var i, max = this[0];
for (i = 1; i < this.length; i++)
{
if (max < this[i])
max = this[i];
}
return max;
}
Array.prototype.max = array_max;
var x = new Array(1, 2, 3, 4, 5, 6);
var y = x.max( );
该代码执行后,y 保存数组 x 中的最大值,或说 6。
所有 JScript 内部对象都有只读的 prototype 属性。可以象该例中那样为原型添加功能,但该对象不能被赋予不同的原型。然而,用户定义的对象可以被赋给新的原型。
本语言参考中每个内部对象的方法和属性列表指出哪些是对象原型的部分,哪些不是。
下面是代码原文
程序代码
<SCRIPT LANGUAGE="JavaScript">
<!--
//出处:网上搜集
//made by yaosansi 2005-12-02
//For more visit http://www.yaosansi.com
// Trim() , Ltrim() , RTrim()
String.prototype.Trim = function()
{
return this.replace(/(^/s*)|(/s*$)/g, "");
}
String.prototype.LTrim = function()
{
return this.replace(/(^/s*)/g, "");
}
String.prototype.RTrim = function()
{
return this.replace(/(/s*$)/g, "");
}
//-->
</SCRIPT>
下面来我们来看看Js脚本中"/s表示什么"
/s 匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [ /f/n/r/t/v]。
请紧记是小写的s
方法二:
由于使用方法简单,所以这里就不举例子了.
//javascript去空格函数
function LTrim(str){ //去掉字符串 的头空格
var i;
for(i=0;i if(str.charAt(i)!=" "&&str.charAt(i)!=" ") break;
}
str = str.substring(i,str.length);
return str;
}
function RTrim(str){
var i;
for(i=str.length-1;i>=0;i--){
if(str.charAt(i)!=" "&&str.charAt(i)!=" ") break;
}
str = str.substring(0,i+1);
return str;
}
function Trim(str){
return LTrim(RTrim(str));
}
方法三:
这个方法将函数写在一起,通过传递参数不同而达到不同的实现效果
<HTML>
<HEAD>
<TITLE>JavaScript Trim Function</TITLE>
<SCRIPT LANGUAGE=javascript> 1:
2: <!--
3: //****************************************************************
4: // Description: sInputString 为输入字符串,iType为类型,分别为
5: // 0 - 去除前后空格; 1 - 去前导空格; 2 - 去尾部空格
6: //****************************************************************
7: function cTrim(sInputString,iType)
8: {
9: var sTmpStr = ' '
10: var i = -1
11:
12: if(iType == 0 || iType == 1)
13: {
14: while(sTmpStr == ' ')
15: {
16: ++i
17: sTmpStr = sInputString.substr(i,1)
18: }
19: sInputString = sInputString.substring(i)
20: }
21:
22: if(iType == 0 || iType == 2)
23: {
24: sTmpStr = ' '
25: i = sInputString.length
26: while(sTmpStr == ' ')
27: {
28: --i
29: sTmpStr = sInputString.substr(i,1)
30: }
31: sInputString = sInputString.substring(0,i+1)
32: }
33: return sInputString
34: }
35: //-->
</SCRIPT>
1:
2: </HEAD>
3:
4: <BODY>
5: JavaScript中的字符串去除空格函数(自定义):<br>
6:
7: <SCRIPT LANGUAGE=javascript>
8: <!--
9: var sR0 = cTrim(" T e s t ",0)
10: var sR1 = cTrim(" T e s t ",1)
11: var sR2 = cTrim(" T e s t ",2)
12: document.write("R0 = '" + sR0 + "'<br>")
13: document.write("R1 = '" + sR1 + "'<br>")
14: document.write("R2 = '" + sR2 + "'<br>")
15: //-->
</SCRIPT>
</BODY>
</HTML>