javascript當(dāng)中反義字符用法

反義字符
馬克- to-win:馬克 java社區(qū):防盜版實(shí)名手機(jī)尾號: 73203。
\W    匹配任意非字母,數(shù)字,下劃線,漢字的字符
\S    匹配任意不是空白符的字符
\D    匹配任意非數(shù)字的字符
\B    匹配不是單詞開頭或結(jié)束的位置
^     取反(^寫在[]里面的話,就代表排除的意思,一般來講,^ 匹配字符串的開始)
用法示例:
1、[^abc]匹配除了abc這幾個字母以外的任意字符
   2、\S+匹配不包含空白符的字符串。




字符轉(zhuǎn)義

如果查找預(yù)定義字符集本身的話我們沒法指定它們,因?yàn)樗鼈儠唤忉尦商厥獾囊馑?。這時你就必須使用\來取消這些字符的特殊意義。
.        用\.
*        用\*
\本身    用\\


例如:kkk\ .com匹配kkk.com
    D:\\Win匹配D:\Win



重復(fù)



*        重復(fù)零次或更多次(任意次)
+        重復(fù)一次或更多次
?        重復(fù)零次或一次
{n}        重復(fù)n次
{n,}    重復(fù)n次或更多次
{n,m}    重復(fù)n到m次




例如:
中國的手機(jī)號:^18\d{9}|15\d{9} $

QQ號(以非0開頭,共5位到9位數(shù)字):^[1-9]\d{4,8}$

電子郵箱:^[0-9a-zA-Z](\w)+@(\w)+(\.)(cn|com|net|gov|edu|com(\.)cn)$


例 3.1.1

<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</head>
<script>
    /* 馬克-to-win:String.match() (Method)
    Searches a string using a regular expression and returns the matches in an array.
    Property/method value type: Array object
    JavaScript syntax: - myObject.match(aPattern)
    */
    var r, reg;
    // 聲明變量。g:global,如果沒有g(shù),只搞一個。 要是g的話, 搞所有的。
    var ss = "all over the world the heavy rain in Spain  falls mainly in the plain";
    reg = /ain/ig;
    // 創(chuàng)建正則表達(dá)式模式。
    r = ss.match(reg);
    // 嘗試去匹配搜索字符串。
    document.write(r[0] + " " + r[1] + " " + r.length + "<br>");

    var s1 = "all over the world the heavy rain in Spain falls mainly in the plain";
    re1 = /qixy/ig;
    // 創(chuàng)建正則表達(dá)式模式。
    r1 = s1.match(re1);
    // 嘗試去匹配搜索字符串。
    document.write("r1 is" + r1);
    if (!r1) alert("r1 is null");

    var ss = " The dfsdf the fsdgl with the gdf.gdfhg the fielder caught the ball with the glove.";
    var req = /the/g;
    var r = ss.replace(req, "A");
    document.write("r is" + r)


</script>



輸出結(jié)果:
ain ain 4
r1 isnullr is The dfsdf A fsdgl with A gdf.gdfhg A fielder caught A ball with A glove.




例 3.1.2

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</head>
<SCRIPT LANGUAGE="JavaScript">
    <!--
    function check()
    {

        var email = document.regFo.email.value;
        /* 馬克-to-win \w 匹配包括下劃線的任何單詞字符。等價于'[A-Za-z0-9_]'。* 匹配前面的子表達(dá)式零次或多次。  + 匹配前面的子表達(dá)式一次或多次。\. means 原形. a typical value is z_a@hx.com.cn
  */
        var regemail = /^[0-9a-zA-Z](\w)*@(\w)+(\.)(com|cn|net|gov|edu|com(\.)cn)$/;
    
        if (!email.match(regemail))
        {
            alert("email格式不對!")
    //    document.regForm.email.value="";
            return false;
        }

        return true;
    }
    //-->
</SCRIPT>
</HEAD>

<BODY>
<FORM name="regFo" METHOD=POST ACTION="abc.jsp" onsubmit="return check()">
    <TABLE>
         <TR>
            <TD>email</TD>
            <TD><INPUT TYPE="text" NAME="email" style="width:150px"></TD>
        </TR>
        <TR>
            <TD><INPUT TYPE="submit" value="提交"></TD>
            <TD><INPUT TYPE="reset"></TD>
        </TR>

    </TABLE>


</FORM>
</BODY>
</HTML>




例 3.1.3



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<SCRIPT LANGUAGE="JavaScript">
<!--
   myString = "java, JAVA, Java";
  //         f= f.replace(/ba|mdd|a|木/g,"*");
/* 馬克-to-win:JavaScript syntax: - myObject.replace(aPattern, aString)
Argument list: aPattern A regular expression pattern
aString A replacement string
g:global,如果沒有g(shù),只替換一個字。 要是g的話, 所有字都替換。有關(guān)正則, 參考書。
*/
   myString=myString.replace(/java/ig, "Javaa");
   document.write(myString);
//-->
</SCRIPT>
</HEAD>
<BODY >

</BODY>
</HTML>


輸出結(jié)果:
Javaa, Javaa, Javaa




例 3.1.4

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</head>
<BODY>
<SCRIPT LANGUAGE="JavaScript">
<!--
String.prototype.trimq = function()
 {
/* 馬克-to-win:\s 匹配任何空白字符,包括空格、制表符、換頁符等等。等價于 [ \f\n\r\t\v]。 * 匹配前面的子表達(dá)式零次或多次。^ 匹配輸入字符串的開始位置
  $ 匹配輸入字符串的結(jié)束位置。note that here we must use ^, if we don't use it, any white place will be removed.   */
    return this.replace(/(^\s*)|(\s*$)/g, "");
 }
    var s="    java cds    ";
/*下面這句效果和后面一樣*/
//    var s2 = s.trimq( );
/* \s 匹配任何空白字符,包括空格、制表符、換頁符等等。等價于 [ \f\n\r\t\v]。 * 匹配前面的子表達(dá)式零次或多次。^ 匹配輸入字符串的開始位置
  $ 匹配輸入字符串的結(jié)束位置。note that here we must use ^, if we don't use it, any white place will be removed.   */
    var s2 = s.replace(/(^\s*)|(\s*$)/g, "");
    document.write("s2 is"+s2+"s2.length is "+s2.length) ;
//-->
</SCRIPT>
</BODY>
</HTML>

輸出結(jié)果:
s2 isjava cdss2.length is 8