易截截图软件、单文件、免安装、纯绿色、仅160KB

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  ,  大致上方法就是这样的,可以实现


相关文档:

javascript运算符优先级

 <P>JScript 中的运算符优先级是一套规则。该规则在计算表达式时控制运算符执行的顺序。具有较高优先级的运算符先于较低优先级的运算符执行。例如,乘法的执行先于加法。</P>
<P>下表按从最高到最低的优先级列出 JScript 运算符。具有相同优先级的运算符按从左至右的顺序求值。</P>
<DIV ......

40个常用网站javascript脚本

1. oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键
<table border oncontextmenu=return(false)><td>no</table> 可用于Table
2. <body onselectstart="return false"> 取消选取、防止复制
3. onpaste="return false" 不准粘贴
4. oncopy="return false;" oncut="return f ......

Javascript 定时器调用传递参数的使用方法(转)

 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 ......

javascript typeof的用法

经常会在js里用到数组,比如 多个名字相同的input, 若是动态生成的, 提交时就需要判断其是否是数组.
if(document.mylist.length != "undefined" ) {} 这个用法有误.
正确的是 if( typeof(document.mylist.length) != "undefined" ) {}
或 if( !isNaN(document.mylist.length) ) {}
typeof的运算数未定义,返回的就是 "u ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号