javascript 替换空格
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 , 大致上方法就是这样的,可以实现
相关文档:
封装:通过闭包才算的上是真正意义上的封装
<script type="text/javascript">
function myInfo(){
var name ="老鱼",age =27;
var myInfo = "my name is" + name + "i am" + age  ......
Javascript 定时器调用传递参数的使用方法
from:txdnet.cn, Date:2008-04-24 08:18:36.0, Hit:317
Tag:0
Do:收藏到IE Google baidu yahoo QQ书签 mySpace 评论(0) 轉爲繁體 大 中 小
无论是window.setTimeout 还是window.setInterval ......
在写读后感之前,先自我介绍一下,本人,男,24岁,未婚,资浅.NET程序员,在读此书之前已经能够熟练的利用JavaScript进行表单的一般验证(通过document.getElementById获取出文本框的值后再进行if...else...判断)。
刚刚拿到此书,心情好一番激动,沃~~~新华字典,长约26CM,宽约18CM,高约3.5 ......
1. 总是使用 ‘var’
在JavaScript
中,变量不是全局范围的就是函数范围的,使用”var”关键词将是保持变量简洁明了的关键。当声明一个或者是全局或者是函数级(function-level)的变量,需总是前置”var”关键词,下面的例子将强调不这样做潜在的问题。
不使用 Var 造成的问题
var i=0; ......