JS正则表达式:
^[1-9]\d*$ 匹配正整数
用法:
<script type="text/javascript">
var test = '11';
alert(test.search(/^[1-9]\d*$/));
</script>
如果不是纯的正整数,那么打印-1,如果为正整数,那么打印0
search 方法
返回与正则表达式查找内容匹配的第一个子字符串的位置。
stringObj.search(rgExp)
参数
stringObj
必选项。要在其上进行查找的 String 对象或字符串文字。
rgExp
必选项。包含正则表达式模式和可用标志的正则表达式对象。
说明
search 方法指明是否存在相应的匹配。
如果找到一个匹配,search 方法将返回一个整数值,指明这个匹配距离字符串开始的偏移位置。
如果没有找到匹配,则返回 -1。
示例
下面的示例演示了 search 方法的用法。
function SearchDemo(){ var r, re; //
声明变量。var s = "The rain in Spain falls mainly in the plain.";
re = /falls/i; //
创建正则表达式模式。r = s.search(re); //
查找字符串。return(r); //
返回Boolean
结果。 }
应用于: String 对象
----------------------------------------------------------------------------------------------------------------------------
http://www.jsdoc.cn/article.do?articleID=1151#
在面向对象编程语言中,对于this关键字我们是非常熟悉的。比如C++、C#和Java等都提供了这个关键字,虽然在开始学习的时候觉得比较难,但只要理解了,用起来是非常方便和意义确定的。JavaScript也提供了这个this关键字,不过用起来就比经典OO语言中要"混乱"的多了。
下面就来看看,在JavaScript中各种this的使用方法有什么混乱之处?
1、在HTML元素事件属性中inline方式使用this关键字:
<div οnclick="
// 可以在里面使用this
">division element</div>
我们一般比较常用的方法是在此使用:javascirpt: EventHandler(this),这样的形式。不过这里其实可以写任何合法的JavaScript语句,要是高兴在此定义个类也可以(不过将会是个内部类)。这里的原理是脚本引擎生成了一个div实例对象的匿名成员方法,而onclick指向这个方法。
2、用DOM方式在事件处理函数中使用this关键字:
<div id="elmtDiv">division element</div>
<script language="javascript">
var div = document.getElementById('elmtDiv');
div.attachEvent('onclick', EventHandler);
function EventHandler()
{
// 在此使用this
}
</script>
这时的EventHandler()方法中的this关键字,指示的对象是IE的window对象。这是因为EventHandler只是一个普通的函数,对于attachEvent后,脚本引擎对它的调用和div对象本身没有任何的关系。同时你可以再看看EventHandler的caller属性,它是等于null的。如果我们要在这个方法中获得div对象引用,应该使用:this.event.srcElement。
3、用DHTML方式在事件处理函数中使用this关键字:
<div id="elmtDiv">division element</div>
<script language="javascript">
var div = document.getElementById('elmtDiv');
div.onclick = function()
{
// 在此使用this
};
</script>
这里的this关键字指示的内容是div元素对象实例,在脚本中使用DHTML方式直接为div.onclick赋值一个EventHandler的方法,等于为div对象实例添加一个成员方法。这种方式和第一种方法的区别是,第一种方法是使用HTML方式,而这里是DHTML方式,后者脚本解析引擎不会再生成匿名方法。
4、类定义中使用this关键字:
function JSClass()
{
var myName = 'jsclass';
this.m_Name = 'JSClass';
}
JSClass.prototype.ToString = function()
{
alert(myName + ', ' + this.m_Name);
};
var jc = new JSClass();
jc.ToString();
这是JavaScript模拟类定义中对this的使用,这个和其它的OO语言中的情况非常的相识。但是这里要求成员属性和方法必须使用this关键字来引用,运行上面的程序会被告知myName未定义。
5、为脚本引擎内部对象添加原形方法中的this关键字:
Function.prototype.GetName = function()
{
var fnName = this.toString();
fnName = fnName.substr(0, fnName.indexOf('('));
fnName = fnName.replace(/^function/, '');
return fnName.replace(/(^\s+)|(\s+$)/g, '');
}
function foo(){}
alert(foo.GetName());
这里的this指代的是被添加原形的类的实例,和4中类定义有些相似,没有什么太特别的地方。
6、结合2&4,说一个比较迷惑的this关键字使用:
function JSClass()
{
this.m_Text = 'division element';
this.m_Element = document.createElement('DIV');
this.m_Element.innerHTML = this.m_Text;
this.m_Element.attachEvent('onclick', this.ToString);
}
JSClass.prototype.Render = function()
{
document.body.appendChild(this.m_Element);
}
JSClass.prototype.ToString = function()
{
alert(this.m_Text);
};
var jc = new JSClass();
jc.Render();
jc.ToString();
我就说说结果,页面运行后会显示:"division element",确定后点击文字"division element",将会显示:"undefined"。
7、CSS的expression表达式中使用this关键字:
[复制此代码]CODE:
<table width="100" height="100">
<tr>
<td>
<div style="width: expression(this.parentElement.width);
height: expression(this.parentElement.height);">
division element</div>
</td>
</tr>
</table>
这里的this看作和1中的一样就可以了,它也是指代div元素对象实例本身。
8、函数中的内部函数中使用this关键字:
[复制此代码]CODE:
function OuterFoo()
{
this.Name = 'Outer Name';
function InnerFoo()
{
var Name = 'Inner Name';
alert(Name + ', ' + this.Name);
}
return InnerFoo;
}
OuterFoo()();
运行结果显示是:"Inner Name, Outer Name"。按我们在2中的讲解,这里的结果如果是"Inner Name, undefined"似乎更合理些吧?但是正确的结果确实是前者,这是由于JavaScript变量作用域的问题决定的,详细了解推荐参看"原来JScript中的关键字'var'还是有文章的"一文及回复。
说了这么多JavaScript中this的用法,其实this最根本的特性还是和OO语言中的定义相吻合的。之所以有这么多看似混乱的使用方式,是因为JavaScript语言(解释器和语言本身的内容)本身在实现上是遵循OO的(Object-based),连它的所有数据类型都是对象,也有Object这样一个super Object。但是这个语言在运行上(runtime),就没有遵循完备的OO特点,所以就出现了this的指代混乱。
JavaScript里还有什么地方有this的使用呢?我暂时能想到的就这些了,欢迎讨论补充。
---------------------------------------------------------------------------------------------------------------------------
<script type="text/javascript">
function kkk(){
var re = /^[1-9]+[0-9]*]*$/;
alert(document.getElementById("te").value);
var ttt = document.getElementById("te").value;
if (!re.test(ttt))
{
alert("please input(例:0.02)");
document.getElementById("te").focus();
return false;
}
}
</script>
<input type="text" id="te" />
<input type="button" onClick="javascript:kkk();">
以上是判断是否为数字或者整数
事件:
点击一个自动的将另外的一个单选框选中:
document.getElementsByName('OnlineTfn')[3].checked ='checked';
数字的检查:
function numberValidate(incomingString, defaultValue)
{
if(trimSpace(incomingString).length == 0 || incomingString.search(/[^0-9\.]/g) != -1 || incomingString==defaultValue || parseInt(incomingString, 10) <= 0 )
{
return false;
}
else
return true;
}
邮件的检查:
function emailValidate(incomingString, defaultValue) {
if(trimSpace(incomingString).length == 0 || incomingString.search
(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i) == -1 || incomingString==defaultValue)
{
return false;
}
else
return true;
}
function trimSpace(x)
{
var emptySpace = / /g;
var trimAfter = x.replace(emptySpace,"");
return(trimAfter);
}
电话号码的检查:
function phoneValidate(incomingString, defaultValue)
{
if(trimSpace(incomingString).length == 0 || incomingString.search(/[^0-9/\-_]/g) != -1 || incomingString==defaultValue || parseInt(incomingString, 10) <0 )
{
return false;
}
else
return true;
}
JS中将运算之后的值合并成一个值:
var x = document.getElementById('i1').value+""+document.getElementById('i2').value+"";
document.getElementById("x").value= x;
以后还会积累,转贴请注明,看贴请回复
如果出现is undifined情况,那么原因就是可能出现了多个重名的function,或者是function的name不正确导致的错误。
如何定位到一个form里面的option
<form action="/sfsaf" method="post" name="musicType">
<tr><td><input type="button" οnclick='javascript:doxx();'></td></tr>
<input type="text" name="studentname">
<input type="submit" value="su">
<select name="music">
<option value="1">1</option>
<option value="1">2</option>
<option value="1">3</option>
<option value="1">4</option>
<option value="1">5</option>
<option value="1">6</option>
<option value="1">7</option>
</select>
alert(document.musicType.music[0].selected);
document---form----select(name)[ ]即可以定位到option了,第几个option即select array的第几个index
定制打开窗口大小:
var reDirectUrl= "xxxxx.do";
window.open(reDirectUrl,"","width=800,height=600,toolbar=no,resizable=no,scrollbars=yes" );
Javascript 实例:
http://tech.sina.com.cn/introduction/focus/javascript.shtml
javascript中的array对象:
http://tieba.baidu.com/p/1478111698
http://www.w3school.com.cn/js/js_howto.asp
Javascript之prototype:
function Person(){
this.name="???";
this.sayHellow= function(){
alert("Hi, I am " + this.name);
}
}
varparentPerson = new Person();
function Student(){
this.name="Ben";
this.age="28";
}
Student.prototype =parentPerson;
new Person().sayHellow();
new Student().sayHellow();
http://www.cnblogs.com/goody9807/archive/2007/04/16/715109.html
一个用js代码生成的Loading效果
setTimeouthttp://www.nowamagic.net/webdesign/webdesign_JavascriptLoading.php
javascript必知必会之this关键字及scope
http://tech.ddvip.com/2009-09/1254140418134661_2.html
javascript prototype
http://tech.ddvip.com/2007-08/118778196832675.html
JS闭包:
http://www.cnblogs.com/zhjjNo1/archive/2011/02/12/1951905.html
function generator(){
var uniqueid = 0;
this.show = function(){
uniqueid++;
return uniqueid;
};
}
var aa = new generator();
alert("generate unique ID========="+ aa.show());
alert("generate unique ID========="+ aa.show());
alert("generate unique ID========="+ aa.show());
</script>
function generate(){
var uniqueid = 1;
var todayDate=new Date();
var year=todayDate.getFullYear();
var date=todayDate.getDate();
var month=todayDate.getMonth()+1;
var hour=todayDate.getHours();
var mininutes=todayDate.getMinutes();
var seconds=todayDate.getSeconds();
var ran=Math.round((Math.random())*10000);
var Unique = ""+year+""+""+month+""+""+date+" "+""+""+hour+""+""+mininutes+""+""+seconds+" "+""+ran+uniqueid;
uniqueid +=1;
return Unique;
}
更改input type:
用户:<input type=text value="ayeah"><BR>
密码:<span id="pass"><input type=password id=pw name="pw" value="www.ayeah.com"></span><INPUT TYPE="checkbox" id="showPass" οnclick="
var temp=document.getElementById('pw').value;
if(this.checked){
document.getElementById('pass').innerHTML='<input type=text id=pw name=pw>';}else{document.getElementById('pass').innerHTML='<input type=password id=pw name=pw>';} document.getElementById('pw').value=temp;">显示密码
正则表达式方法
function checkRate(input)
{
var re = /^[0-9]+.?[0-9]*$/; //判断字符串是否为数字 //判断正整数 /^[1-9]+[0-9]*]*$/
if (!re.test(input.rate.value))
{
alert("请输入数字(例:0.02)");
input.rate.focus();
return false;
}
}
下面为普通函数写法
function BASEisNotNum(theNum)
{
//判断是否为数字
if (BASEtrim(theNum)=="")
return true;
for(var i=0;i<theNum.length;i++){
oneNum=theNum.substring(i,i+1);
if (oneNum<"0" || oneNum>"9")
return true;
}
return false;
}
function BASEisNotInt(theInt)
{
//判断是否为整数
theInt=BASEtrim(theInt);
if ((theInt.length>1 && theInt.substring(0,1)=="0") || BASEisNotNum(theInt)){
return true;
}
return false;
}
{
//判断是否为浮点数
len=theFloat.length;
dotNum=0;
if (len==0)
return true;
for(var i=0;i<len;i++){
oneNum=theFloat.substring(i,i+1);
if (oneNum==".")
dotNum++;
if ( ((oneNum<"0" || oneNum>"9") && oneNum!=".") || dotNum>1)
return true;
}
if (len>1 && theFloat.substring(0,1)=="0"){
if (theFloat.substring(1,2)!=".")
return true;
}
return false;
自动跳转:
<body>
<table>
<tr><td>
<form name="frr" method="post" action="/AUSCP/ICARD/apps/scp/campaign_gateway/terms_conditions.do">
</form>
</td></tr>
<script language="javascript" type="text/javascript">
document.getElementsByName("frr")[0].submit();
</script>
</table>
<table><tr>
</tr></table>
</body>
也可以
windows.location.href="";
exec 方法
用正则表达式模式在字符串中运行查找,并返回包含该查找结果的一个数组。
rgExp.exec(str)
参数
rgExp 必选项。包含正则表达式模式和可用标志的正则表达式对象。
str 必选项。要在其中执行查找的 String 对象或字符串文字。
说明
如果 exec 方法没有找到匹配,则它返回 null。如果它找到匹配,则 exec 方法返回一个数组,并且更新全局 RegExp 对象的属性,以反映匹配结果。数组的0元素包含了完整的匹配,而第1到n元素中包含的是匹配中出现的任意一个子匹配。这相当于没有设置全局标志 (g) 的match 方法。
如果为正则表达式设置了全局标志,exec 从以 lastIndex 的值指示的位置开始查找。如果没有设置全局标志,exec 忽略lastIndex 的值,从字符串的起始位置开始搜索。
exec 方法返回的数组有三个属性,分别是 input、index 和lastIndex。Input属性包含了整个被查找的字符串。Index 属性中包含了整个被查找字符串中被匹配的子字符串的位置。LastIndex 属性中包含了匹配中最后一个字符的下一个位置。
<script language="JavaScript">
function ge(obj)
{
re=/(/w+)/s(/d+)/;
re.exec(obj.value);
window.alert(RegExp.$1+"您的年龄是"+this.form1.ag.value+RegExp.$2);
}
</script>
打开pdf:
function doPopup(url)
<a href="javascript:doPopup('/ICARD/download/common/CardGuard_PDS.pdf')" class="applink">Product Disclosure Statementthe Product Disclosure Statement</a>
--------------------------
js-----exec方法
exec 方法
用正则表达式模式在字符串中运行查找,并返回包含该查找结果的一个数组。
rgExp.exec(str)
参数
rgExp 必选项。包含正则表达式模式和可用标志的正则表达式对象。
str 必选项。要在其中执行查找的 String 对象或字符串文字。
说明
如果 exec 方法没有找到匹配,则它返回 null。如果它找到匹配,则 exec 方法返回一个数组,并且更新全局 RegExp 对象的属性,以反映匹配结果。数组的0元素包含了完整的匹配,而第1到n元素中包含的是匹配中出现的任意一个子匹配。这相当于没有设置全局标志 (g) 的 match 方法。
如果为正则表达式设置了全局标志,exec 从以 lastIndex 的值指示的位置开始查找。如果没有设置全局标志,exec 忽略 lastIndex 的值,从字符串的起始位置开始搜索。
exec 方法返回的数组有三个属性,分别是 input、index 和 lastIndex。Input 属性包含了整个被查找的字符串。Index 属性中包含了整个被查找字符串中被匹配的子字符串的位置。LastIndex 属性中包含了匹配中最后一个字符的下一个位置。
<script language="JavaScript">
function ge(obj)
{
re=/(/w+)/s(/d+)/;
re.exec(obj.value);
window.alert(RegExp.$1+"您的年龄是"+this.form1.ag.value+RegExp.$2);
}
</script>