一. regular expressions
1.
var customer = "Alan Turing 555-1212";var pattern = /(\w+) \w+ ([\d-]{8})/;pattern.test(customer);
Since the pattern contained parentheses that created two subexpressions, \w+ and [\d-]{8}, we can reference the two substrings they match, “Alan” and “555-1212,” individually. Substrings accessed in this manner are numbered from left to right, beginning with $1 and ending typically with $9. For example,
var customer = "Alan Turing 555-1212";var pattern = /(\w+) \w+ ([\d-]{8})/;if (pattern.test(customer)) alert("RegExp.$1 = " + RegExp.$1 + "\nRegExp.$2 = " + RegExp.$2);
2.
var pattern = /cat/;var result = pattern.exec("He is a big cat, a fat black cat named Rufus.");
document.writeln("result.length = "+result.length+"<<br />>");
document.writeln("result.index = "+result.index+"<<br />>");
document.writeln("result.lastIndex = "+result.lastIndex+"<<br />>");
document.writeln("result.input = "+result.input+"<<br />>");
the result:
二.
1.下面的JavaScript代码把浏览器的类型保存到ie变量:
applnname=navigator.appName; if(applnname=="Microsoft Internet Explorer") { ie=true; } else { ie=false; }
var splitExp =/\|/; //分隔符表达式
var str = somestr.spilit(splitExp); //得到一个分割后的字符数组;
###
//清空Form中内容只有空格的文本框
function clearSpaceText(form)
{
var length=form.all.length;
for(i=0;i<length;i++){
var textbox=form.all[i];
if(textbox.type=="text"){
if(trim(textbox.value)=="")
textbox.value="";
}
}
}
function trim(str){
if(str==null) return "";
if(str.length==0) return "";
var i=0,j=str.length-1,c;
for(;i<str.length;i++){
c=str.charAt(i);
if(c!=' ') break;
}
for(;j>-1;j--){
c=str.charAt(j);
if(c!=' ') break;
}
if(i>j) return "";
return str.substring(i,j+1);
}
function getStringLength(str){
var endvalue=0;
var sourcestr=new String(str);
var tempstr;
for (var strposition = 0; strposition < sourcestr.length; strposition ++) {
tempstr=sourcestr.charAt(strposition);
if (tempstr.charCodeAt(0)>255 || tempstr.charCodeAt(0)<0) {
endvalue=endvalue+2;
} else {
endvalue=endvalue+1;
}
}
return(endvalue);
}
####
function validateDate(form)
{
var time1=trim(form.sailTime.value);
var time2=trim(form.foreseeTime.value);
if(time1=="") {theMsg+= ++i + ". 请输入起航时间\n"; return; }
var reg="HHmm";
var reg=reg.replace(/yyyy/,"[0-9]{4}");
var reg=reg.replace(/yy/,"[0-9]{2}");
var reg=reg.replace(/MM/,"((0[1-9])|1[0-2])");
var reg=reg.replace(/M/,"(([1-9])|1[0-2])");
var reg=reg.replace(/dd/,"((0[1-9])|([1-2][0-9])|30|31)");
var reg=reg.replace(/d/,"([1-9]|[1-2][0-9]|30|31))");
var reg=reg.replace(/HH/,"(([0-1][0-9])|20|21|22|23)");
var reg=reg.replace(/H/,"([0-9]|1[0-9]|20|21|22|23)");
var reg=reg.replace(/mm/,"([0-5][0-9])");
var reg=reg.replace(/m/,"([0-9]|([1-5][0-9]))");
var reg=reg.replace(/ss/,"([0-5][0-9])");
var reg=reg.replace(/s/,"([0-9]|([1-5][0-9]))");
reg=new RegExp("^"+reg+"$");
if(reg.test(time1)==false){//验证格式是否合法
{ theMsg+= ++i + ". 起航时间格式不正确\n"; form.sailTime.style.color="red"; }
}
if(time2!="")
{
if(reg.test(time2)==false)
{theMsg+= ++i + ". 预计到达时间格式不正确\n"; form.foreseeTime.style.color="red";}
}
}
###
function test(){
var stringwithdelimits = "10 / 3 / / 4 / 7 / 9";
var splitExp = /[ \/]+/;
var array = stringwithdelimits.split(splitExp);
for(var i=0;i<array.length;i++){
alert(array[i]);
}
}
*********************************
<form name=".." .. onsubmit="return submitClick();">
<input type="submit" value="提交"/>
</form>
<script>
function submitClick(){
if(..) return false; //不提交form
return true; //提交form
}
</script>
*********************************
var a = document.getElementById("someid");
取不到的话a 为null