JavaScript 正则表达式和全局对象

来源:互联网 发布:2m数据测试仪 编辑:程序博客网 时间:2024/05/23 16:53
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>正则表达式</title></head><body>    <script>    //修饰符    //i 对大小不敏感的匹配 g 执行全局匹配 m 执行多行匹配                                             //[]查找中括号之间的任何字符类如[a-c]    var str="hello java! c++ js";    var patt1=/[a-z]/g;    document.write(str.match(patt1));    //[^a-c]查找任何不在中括号中得值    var patt2=/[^a-c]/g;    document.write("<br>");    document.write(str.match(patt2));    //[0-9]查找所有从0-9的数字    document.write("<br>");    var s="15454 5absd55  aas a cdg , as+ASXSX-asfd saf";    var p=/[0-9]/g;    document.write(s.match(p));    //[a-z]小写所有字母[A-Z]大写的所有字母    //[A-z]查找所以的字母    document.write("<br>");    var p1=/[A-Z]/g;    document.write(s.match(p1));    var p2=/[a-z]/g;    document.write(s.match(p2));    var p3=/[A-z]/g;    document.write(s.match(p3));    //[元素]查找指定元素    document.write("<br>");    var p4=/[ads]/g;    document.write(s.match(p4));    //[^元素]查找不再集合中的元素    document.write("<br>");    var p5=/[^asc]/g;    document.write(s.match(p5));    //    document.write("<br>");    var p6=/red|as|a/g;    document.write(s.match(p6));    //元字符    //.查找点两边元素包含得值    document.write("<br>");    var s1=/a.s/g;    document.write(s.match(s1));    //\w查找单词字符    document.write("<br>");    var s2=/\w/g;    document.write(s.match(s2));    //\W查找非单词字符。    document.write("<br>");    var s3=/\W/g;    document.write(s.match(s3));    //查找数字。    document.write("<br>");    var s4=/\d/g;    document.write(s.match(s4));    // \D   查找非数字字符。    document.write("<br>");    var s5=/\D/g;    document.write(s.match(s5));    // \s   查找空白字符    document.write("<br>");    var s6=/\s/g;    document.write(s.match(s6));    //\S查找非空白字符。    document.write("<br>");    var s7=/\S/g;    document.write(s.match(s7));    // \b   匹配单词边界。    document.write("<br>");    var s8=/\bs/g;    document.write(s.match(s8));    // \D       匹配非单词边界。    document.write("<br>");    var s9=/\D/g;    document.write(s.match(s9));    // \0   查找 NUL 字符。    document.write("<br>");    var s10=/\0/g;    document.write(s.match(s10));    // \n   查找换行符。    document.write("<br>");    var s11=/\n/g;    document.write(s.match(s11));    // \f   查找换页符。    document.write("<br>");    var s12=/\f/g;    document.write(s.match(s12));    //\r    查找回车符    document.write("<br>");    var s13=/\r/g;    document.write(s.match(s13));    document.write("<br>");    //\t查找制表符    document.write("<br>");    var s14=/\t/;    document.write(s.match(s14));    //\v查找垂直制表符    document.write("<br>");    var s15=/\v/g;    document.write(s.match(s15));    //\xxx查找以八进制数 xxx 规定的字符。    document.write("<br>");    var s16=/\152/g;    document.write(s.match(s16));    //\xdd查找以十六进制数 dd 规定的字符。    document.write("<br>");    var s17=/\a15/g;    document.write(s.match(s17));    //\uxxxx查找以十六进制数 xxxx 规定的 Unicode 字符。    document.write("<br>");    var s18 =/\u0057/g;    document.write(s.match(s18));    //量词    //元素+ 匹配任何包含至少一个 n 的字符串。    document.write("<br>");    var a1=/n+/g;    document.write(s.match(a1));    //元素*匹配任何包含零个或多个 n 的字符串。    document.write("<br>");    var a2=/s*/g;    document.write(s.match(a2));    //元素?匹配任何包含零个或一个 n 的字符串。    document.write("<br>");    var a3=/\c?/g;    document.write(s.match(a3));    //n{x}匹配包含 X 个 n 的序列的字符串。    document.write("<br>");    var a4=/a{2}/;    document.write(s.match(a4));    //n{x,y}匹配包含 X 至 Y 个 n 的序列的字符串。    document.write("<br>");    //var a5=/\a{3,2}/g;    //document.write(s.match(a5));    //n{x,}匹配包含至少 X 个 n 的序列的字符串。    document.write("<br>");    var a6=/\a{3,}/g;    document.write(s.match(a6));    //元素$匹配任何结尾为 n 的字符串。    document.write("<br>");    var a7=/\s$/g;    document.write(s.match(a7));    //^元素匹配任何开头为 n 的字符串。    document.write("<br>");    var a8=/\^a/g;    document.write(s.match(a8));    //?=n匹配任何其后紧接指定字符串 n 的字符串。    document.write("<br>");    var a9=/\a(?=a11)/g;    document.write(s.match(a9));    //JavaScript decodeURI() 函数    //  解码某个编码的 URI。    document.write("<br>");    var text1="http://www.w3school.com.cn/My first/";    document.write(encodeURI(text1)+"<br>");    document.write(decodeURI(text1));    //  解码一个编码的 URI 组件。    //JavaScript decodeURIComponent() 函数    document.write("<br>");    var t="http://www.w3school.com.cn/My first/";    document.write(encodeURIComponent(t)+"<br>");    document.write(decodeURIComponent(t));    //  把字符串编码为 URI。    //encodeURI()    document.write("<br>");    document.write(encodeURI("http://www.w3school.com")+"<br>");    document.write(encodeURI("http://www.w3school.com.cn/My first/"));    //document.write(encodeURI(:;/?:@&=+$,#));    //把字符串编码为 URI 组件    //JavaScript encodeURIComponent() 函数    document.write("<br>");    document.write(encodeURIComponent("http://www.w3school.com.cn")+"<br>");    document.write(encodeURIComponent("http://www.w3school.com.cn/p 1/")+"<br>");    document.write(encodeURIComponent(",/?:@&=+$#"))    //      对字符串进行编码    //JavaScript escape() 函数    document.write("<br>");    document.write(escape("Visit w3School!")+"<br>");    //计算 JavaScript 字符串,并把它作为脚本代码来执行    //JavaScript eval() 函数    document.write("<br>");    document.write(eval("3.2-50-56*100+(-200)"));    //返回一个 JavaObject 的 JavaClass。    //JavaScript getClass() 函数    document.write("<br>");    //var o=new java.lang.Class();    //var cls=getClass(o);    //document.write(cls);    //  检查某个值是否为有穷大的数。    //JavaScript isFinite() 函数    document.write("<br>");    document.write(isFinite(123));    </script></body></html>
原创粉丝点击