﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>IT博客-A JavaScript Fancier-随笔分类-正则表达式专题</title><link>http://www.cnitblog.com/yemoo/category/4648.html</link><description>伟大的javascript技术研究中...</description><language>zh-cn</language><lastBuildDate>Mon, 26 Sep 2011 06:34:57 GMT</lastBuildDate><pubDate>Mon, 26 Sep 2011 06:34:57 GMT</pubDate><ttl>60</ttl><item><title>JavaScript之正则表达式 replace </title><link>http://www.cnitblog.com/yemoo/archive/2007/07/19/30231.html</link><dc:creator>Yemoo'S JS Blog</dc:creator><author>Yemoo'S JS Blog</author><pubDate>Thu, 19 Jul 2007 02:04:00 GMT</pubDate><guid>http://www.cnitblog.com/yemoo/archive/2007/07/19/30231.html</guid><wfw:comment>http://www.cnitblog.com/yemoo/comments/30231.html</wfw:comment><comments>http://www.cnitblog.com/yemoo/archive/2007/07/19/30231.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/yemoo/comments/commentRss/30231.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/yemoo/services/trackbacks/30231.html</trackback:ping><description><![CDATA[<script type="text/javascript" src="http://www.cnitblog.com/Files/yemoo/gg1.js"></script><p>在js中使用replace实现正则替换方法为：<br>obj.replace(/xxxx/ig,function(){<br>//xxxxx<br>});<br><br>其执行过程为每次匹配到一个字符串则执行一次第二个参数的function函数。<br>其中 function的参数是这样的：<br>&nbsp;&nbsp;arguments[0], 匹配的串源<br>&nbsp;&nbsp;arguments[1] ... arguments[arguments.length -3]，匹配()内容，相当于常用的 ＄1, ＄2...<br>&nbsp;&nbsp;arguments[arguments.length -2]，匹配串的位置偏移 offset<br>&nbsp;&nbsp;arguments[arguments.length -1]，整个字符串源<br><br>可以用以下脚本去测试：<br></p>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #0000ff">var</span><span style="COLOR: #000000">&nbsp;j</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br></span><span style="COLOR: #0000ff">var</span><span style="COLOR: #000000">&nbsp;str</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">document.write('xxxxxxx');document.write('aaaa')</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br>str.replace(</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">document.write\((.</span><span style="COLOR: #000000">*?</span><span style="COLOR: #000000">)\)</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">ig,</span><span style="COLOR: #0000ff">function</span><span style="COLOR: #000000">(){<br>&nbsp;&nbsp;&nbsp;&nbsp;alert(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">找到第</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">(j</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">个匹配字符串</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">for</span><span style="COLOR: #000000">(</span><span style="COLOR: #0000ff">var</span><span style="COLOR: #000000">&nbsp;i</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">;i</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">arguments.length;i</span><span style="COLOR: #000000">++</span><span style="COLOR: #000000">){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">arguments[</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">i</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">]=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">arguments[i]);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>});</span></div>
<textarea id=sc111111 style="WIDTH: 98%; HEIGHT: 75px" rows=1 cols=41>&lt;script type="text/javascript"&gt;
var j=1;
var str="document.write('xxxxxxx');document.write('aaaa')";
str.replace(/document.write\((.*?)\)/ig,function(){
alert("找到第"+(j++)+"个匹配字符串");
for(var i=0;i&lt;arguments.length;i++){
alert("arguments["+i+"]="+arguments[i]);
}
});
&lt;/script&gt;</textarea> <br /><input onclick="var obj=document.getElementById('sc111111');var win=window.open();win.document.open();win.document.write(obj.value);win.document.close()" type=button value=点击运行><br>======================================================================<br>
<h1>replace 方法</h1>
<p>返回根据正则表达式进行文字替换后的字符串的复制。</p>
<p><code>stringObj.<strong>replace(</strong>rgExp, replaceText<strong>)</strong></code></p>
<h4>参数</h4>
<p class=dt>stringObj </p>
<p class=indent>必选项。要执行该替换的 <strong>String</strong> 对象或字符串文字。该字符串不会被 <strong>replace</strong> 方法修改。 </p>
<p class=dt>rgExp </p>
<p class=indent>必选项。为包含正则表达式模式或可用标志的<strong>正则表达式</strong>对象。也可以是 <strong>String</strong> 对象或文字。如果 <em>rgExp </em>不是<strong>正则表达式</strong>对象，它将被转换为字符串，并进行精确的查找；不要尝试将字符串转化为正则表达式。</p>
<p class=dt>replaceText </p>
<p class=indent>必选项。是一个<strong>String</strong> 对象或字符串文字，对于<em>stringObj</em> 中每个匹配 <em>rgExp</em> 中的位置都用该对象所包含的文字加以替换。在 Jscript 5.5 或更新版本中，<em>replaceText</em> 参数也可以是返回替换文本的函数。</p>
<h4>说明</h4>
<p><strong>replace</strong> 方法的结果是一个完成了指定替换的 <em>stringObj</em> 对象的复制。 </p>
<p>下面任意的匹配变量都能用来识别最新的匹配以及找出匹配的字符串。在需要动态决定替换字符串的文本替换中可以使用匹配变量。</p>
<table cellSpacing=0 cols=2 rules=all border=1 frame=box>
    <tbody>
        <tr vAlign=top>
            <th width="20%">字符</th>
            <th width="80%">含义</th>
        </tr>
        <tr vAlign=top>
            <td width="20%"><strong>$$</strong></td>
            <td width="80%"><strong>$</strong> （JScript 5.5 或更新版本）</td>
        </tr>
        <tr vAlign=top>
            <td width="20%"><strong>$&amp;</strong></td>
            <td width="80%">指定与整个模式匹配的 <em>stringObj</em> 的部分。 （JScript 5.5 或更新版本）</td>
        </tr>
        <tr vAlign=top>
            <td width="20%"><strong>$`</strong></td>
            <td width="80%">指定由 <strong>$&amp;</strong> 描述的匹配之前的 <em>stringObj</em> 部分。 （JScript 5.5 或更新版本）</td>
        </tr>
        <tr vAlign=top>
            <td width="20%"><strong>$'</strong></td>
            <td width="80%">指定由 <strong>$&amp;</strong> 描述的匹配之后的 <em>stringObj</em> 部分。 （JScript 5.5 或更新版本）</td>
        </tr>
        <tr vAlign=top>
            <td width="20%"><strong>$<em>n</em></strong></td>
            <td width="80%">捕获的第 <em>n</em> 个子匹配，此处 <em>n</em> 为从1到9的十进制一位数。 （JScript 5.5 或更新版本）</td>
        </tr>
        <tr vAlign=top>
            <td width="20%"><strong>$<em>nn</em></strong></td>
            <td width="80%">捕获的第 <em>nn</em> 个子匹配，此处 <em>nn</em> 为从01到99的十进制两位数。 （JScript 5.5 或更新版本）</td>
        </tr>
    </tbody>
</table>
<br>
<p><span style="COLOR: #ff0000">如果 <em>replaceText</em> 为函数，对于每一个匹配的子字符串，调用该函数时带有下面的 m+3 个参数，此处 m 是在 <em>rgExp</em> 中捕获的左括弧的个数。第一个参数是匹配的子字符串。接下来的 <em>m</em> 个参数是查找中捕获的全部结果。第 <em>m</em>+2 个参数是在 <em>stringObj</em> 中匹配出现的偏移量，而第 <em>m</em>+3 个参数为 <em>stringObj</em>。结果为将每一匹配的子字符串替换为函数调用的相应返回值的字符串值。</span></p>
<p><strong>Replace </strong>方法更新全局 <strong>RegExp</strong> 对象的属性。</p>
<script type="text/javascript" src="http://www.cnitblog.com/Files/yemoo/gg2.js"></script>
===========================================================     <img src ="http://www.cnitblog.com/yemoo/aggbug/30231.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/yemoo/" target="_blank">Yemoo'S JS Blog</a> 2007-07-19 10:04 <a href="http://www.cnitblog.com/yemoo/archive/2007/07/19/30231.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>写了一个还原html代码的正则</title><link>http://www.cnitblog.com/yemoo/archive/2007/01/24/22269.html</link><dc:creator>Yemoo'S JS Blog</dc:creator><author>Yemoo'S JS Blog</author><pubDate>Wed, 24 Jan 2007 07:25:00 GMT</pubDate><guid>http://www.cnitblog.com/yemoo/archive/2007/01/24/22269.html</guid><wfw:comment>http://www.cnitblog.com/yemoo/comments/22269.html</wfw:comment><comments>http://www.cnitblog.com/yemoo/archive/2007/01/24/22269.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cnitblog.com/yemoo/comments/commentRss/22269.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/yemoo/services/trackbacks/22269.html</trackback:ping><description><![CDATA[<script type="text/javascript" src="http://www.cnitblog.com/Files/yemoo/gg1.js"></script>  <p>今天看到蓝色有人 问这个问题，偶就试着写了一个，感觉正则的replace+function方式挺方便简洁，代码如下：<br /></p>
  <textarea id="code1" style="WIDTH: 737px; HEIGHT: 120px" rows="4" cols="80">&lt;script language="JavaScript" type="text/javascript"&gt;
&lt;!--
    var str='&amp;lt;font color="red"&amp;gt;&amp;lt;/font&amp;gt;';
    str=str.replace(/(&amp;gt;|&amp;lt;)/ig,function(s){return s=="&amp;lt;"?"&lt;":"&gt;"});
    alert(str);
//--&gt;
&lt;/script&gt;</textarea>
  <input onclick="var a=window.open('','','');a.document.write(document.getElementById('code1').value);a.document.close()" type="button" value="运行" />
 <script type="text/javascript" src="http://www.cnitblog.com/Files/yemoo/gg2.js"></script><img src ="http://www.cnitblog.com/yemoo/aggbug/22269.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/yemoo/" target="_blank">Yemoo'S JS Blog</a> 2007-01-24 15:25 <a href="http://www.cnitblog.com/yemoo/archive/2007/01/24/22269.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一个正则实现文本框中只能输入数字和小数点</title><link>http://www.cnitblog.com/yemoo/archive/2007/01/17/21993.html</link><dc:creator>Yemoo'S JS Blog</dc:creator><author>Yemoo'S JS Blog</author><pubDate>Wed, 17 Jan 2007 12:32:00 GMT</pubDate><guid>http://www.cnitblog.com/yemoo/archive/2007/01/17/21993.html</guid><wfw:comment>http://www.cnitblog.com/yemoo/comments/21993.html</wfw:comment><comments>http://www.cnitblog.com/yemoo/archive/2007/01/17/21993.html#Feedback</comments><slash:comments>17</slash:comments><wfw:commentRss>http://www.cnitblog.com/yemoo/comments/commentRss/21993.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/yemoo/services/trackbacks/21993.html</trackback:ping><description><![CDATA[<script type="text/javascript" src="http://www.cnitblog.com/Files/yemoo/gg1.js"></script>
<br><br>学习正则的一个实践产品:<img height=19 src="http://www.cnitblog.com/Emoticons/red_smile.gif" width=19 border=0><br>&lt;input type="text" size="12" onpropertychange="this.value=this.value.replace(/[^\d\.]+?/g,'')" /&gt;&nbsp;<br><br><br>应大家的建议，我又写了一个，试试这个<br><br>&nbsp; <textarea id=testReg style="WIDTH: 730px; HEIGHT: 55px" rows=1 cols=46>&lt;input type="text" size="12" onpropertychange="if(!/^\d*(\.\d*)?$/.test(this.value))this.value=this.value.substr(0,this.value.length-1)" onpaste="return false" /&gt; </textarea><br><input onclick="window.open().document.write(document.getElementById('testReg').value);" type=button value=运行> <script type="text/javascript" src="http://www.cnitblog.com/Files/yemoo/gg2.js"></script><img src ="http://www.cnitblog.com/yemoo/aggbug/21993.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/yemoo/" target="_blank">Yemoo'S JS Blog</a> 2007-01-17 20:32 <a href="http://www.cnitblog.com/yemoo/archive/2007/01/17/21993.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一个准确检测是否是整数的正则</title><link>http://www.cnitblog.com/yemoo/archive/2007/01/07/21623.html</link><dc:creator>Yemoo'S JS Blog</dc:creator><author>Yemoo'S JS Blog</author><pubDate>Sun, 07 Jan 2007 15:53:00 GMT</pubDate><guid>http://www.cnitblog.com/yemoo/archive/2007/01/07/21623.html</guid><wfw:comment>http://www.cnitblog.com/yemoo/comments/21623.html</wfw:comment><comments>http://www.cnitblog.com/yemoo/archive/2007/01/07/21623.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.cnitblog.com/yemoo/comments/commentRss/21623.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/yemoo/services/trackbacks/21623.html</trackback:ping><description><![CDATA[ <script type="text/javascript" src="http://www.cnitblog.com/Files/yemoo/gg1.js"></script> <p>废话少说，看代码：</p>
  <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
    <span style="COLOR: #0000ff">function</span>
    <span style="COLOR: #000000">&#160;&#160;isInteger(strInteger)&#160;&#160;{&#160;&#160;<br />&#160;&#160;&#160;&#160;&#160;&#160;</span>
    <span style="COLOR: #0000ff">var</span>
    <span style="COLOR: #000000">&#160;&#160;newPar</span>
    <span style="COLOR: #000000">=/^</span>
    <span style="COLOR: #000000">(</span>
    <span style="COLOR: #000000">-</span>
    <span style="COLOR: #000000">&#160;&#160;</span>
    <span style="COLOR: #000000">|</span>
    <span style="COLOR: #000000">\</span>
    <span style="COLOR: #000000">+</span>
    <span style="COLOR: #000000">)</span>
    <span style="COLOR: #000000">?</span>
    <span style="COLOR: #000000">\d</span>
    <span style="COLOR: #000000">+</span>
    <span style="COLOR: #000000">$</span>
    <span style="COLOR: #000000">/</span>
    <span style="COLOR: #000000">&#160;&#160;<br />&#160;&#160;&#160;&#160;&#160;&#160;</span>
    <span style="COLOR: #0000ff">return</span>
    <span style="COLOR: #000000">&#160;&#160;newPar.test(strInteger);&#160;&#160;<br />&#160;&#160;&#160;&#160;&#160;}&#160;</span>
  </div>这段代码支持判断正负数，是俺见过的判断数字的比较准确的正则写法。 
<script type="text/javascript" src="http://www.cnitblog.com/Files/yemoo/gg2.js"></script><img src ="http://www.cnitblog.com/yemoo/aggbug/21623.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/yemoo/" target="_blank">Yemoo'S JS Blog</a> 2007-01-07 23:53 <a href="http://www.cnitblog.com/yemoo/archive/2007/01/07/21623.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JS正则解析算术表达式【51js】</title><link>http://www.cnitblog.com/yemoo/archive/2006/12/12/20389.html</link><dc:creator>Yemoo'S JS Blog</dc:creator><author>Yemoo'S JS Blog</author><pubDate>Mon, 11 Dec 2006 16:35:00 GMT</pubDate><guid>http://www.cnitblog.com/yemoo/archive/2006/12/12/20389.html</guid><wfw:comment>http://www.cnitblog.com/yemoo/comments/20389.html</wfw:comment><comments>http://www.cnitblog.com/yemoo/archive/2006/12/12/20389.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/yemoo/comments/commentRss/20389.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/yemoo/services/trackbacks/20389.html</trackback:ping><description><![CDATA[ <script type="text/javascript" src="http://www.cnitblog.com/Files/yemoo/gg1.js"></script> <p>用于解析表达式，有点像编译器里的东东，<br />如对&#8220;(A1+2-1)/2*5&#8221;进行拆分。A1为变量不能分开。<br />有人这样做：</p>
  <div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
    <span style="COLOR: #000000">&lt;</span>
    <span style="COLOR: #000000">script</span>
    <span style="COLOR: #000000">&gt;</span>
    <span style="COLOR: #000000">
      <br />
    </span>
    <span style="COLOR: #0000ff">var</span>
    <span style="COLOR: #000000">&#160;str</span>
    <span style="COLOR: #000000">=</span>
    <span style="COLOR: #000000">"</span>
    <span style="COLOR: #000000">(A1+2-1)/2*5</span>
    <span style="COLOR: #000000">"</span>
    <span style="COLOR: #000000">
      <br />
    </span>
    <span style="COLOR: #0000ff">var</span>
    <span style="COLOR: #000000">&#160;arr</span>
    <span style="COLOR: #000000">=</span>
    <span style="COLOR: #000000">str.match(</span>
    <span style="COLOR: #000000">/</span>
    <span style="COLOR: #000000">\(</span>
    <span style="COLOR: #000000">|</span>
    <span style="COLOR: #000000">\)</span>
    <span style="COLOR: #000000">|</span>
    <span style="COLOR: #000000">\</span>
    <span style="COLOR: #000000">+|</span>
    <span style="COLOR: #000000">\</span>
    <span style="COLOR: #000000">-|</span>
    <span style="COLOR: #000000">\</span>
    <span style="COLOR: #000000">*|</span>
    <span style="COLOR: #000000">\</span>
    <span style="COLOR: #000000">/|</span>
    <span style="COLOR: #000000">[</span>
    <span style="COLOR: #000000">^</span>
    <span style="COLOR: #000000">\(\)\</span>
    <span style="COLOR: #000000">+</span>
    <span style="COLOR: #000000">\</span>
    <span style="COLOR: #000000">-</span>
    <span style="COLOR: #000000">\</span>
    <span style="COLOR: #000000">*</span>
    <span style="COLOR: #000000">\</span>
    <span style="COLOR: #000000">/</span>
    <span style="COLOR: #000000">]</span>
    <span style="COLOR: #000000">+/</span>
    <span style="COLOR: #000000">ig);<br />alert(arr.join('\n'))<br /></span>
    <span style="COLOR: #000000">&lt;/</span>
    <span style="COLOR: #000000">script</span>
    <span style="COLOR: #000000">&gt;</span>
  </div>
  <br />但后面见到写的更好的方法：<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">script</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">var</span><span style="COLOR: #000000">&#160;str</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">(A_1+2j-1)/2*5</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">var</span><span style="COLOR: #000000">&#160;arr</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">str.match(</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">\W</span><span style="COLOR: #000000">|</span><span style="COLOR: #000000">\w</span><span style="COLOR: #000000">+/</span><span style="COLOR: #000000">ig);<br />alert(arr.join('\n'))<br /></span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">script</span><span style="COLOR: #000000">&gt;</span></div><font color="#ff0000">字母和数字可以用\w<br /><br /></font><font color="#000000">收录下来，以便以后查看。</font> 
<script type="text/javascript" src="http://www.cnitblog.com/Files/yemoo/gg2.js"></script><img src ="http://www.cnitblog.com/yemoo/aggbug/20389.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/yemoo/" target="_blank">Yemoo'S JS Blog</a> 2006-12-12 00:35 <a href="http://www.cnitblog.com/yemoo/archive/2006/12/12/20389.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>javascript判断是否含有汉字</title><link>http://www.cnitblog.com/yemoo/archive/2006/07/02/13151.html</link><dc:creator>Yemoo'S JS Blog</dc:creator><author>Yemoo'S JS Blog</author><pubDate>Sun, 02 Jul 2006 11:53:00 GMT</pubDate><guid>http://www.cnitblog.com/yemoo/archive/2006/07/02/13151.html</guid><wfw:comment>http://www.cnitblog.com/yemoo/comments/13151.html</wfw:comment><comments>http://www.cnitblog.com/yemoo/archive/2006/07/02/13151.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.cnitblog.com/yemoo/comments/commentRss/13151.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/yemoo/services/trackbacks/13151.html</trackback:ping><description><![CDATA[<script type="text/javascript" src="http://www.cnitblog.com/Files/yemoo/gg1.js"></script><br />if(/[^\x00-\xff]/g.test(s)){<br>&nbsp;&nbsp;&nbsp; alert("含有汉字");<br>} else {<br>&nbsp;&nbsp;&nbsp; alert("全是字符");<br>} 
<script type="text/javascript" src="http://www.cnitblog.com/Files/yemoo/gg2.js"></script><img src ="http://www.cnitblog.com/yemoo/aggbug/13151.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/yemoo/" target="_blank">Yemoo'S JS Blog</a> 2006-07-02 19:53 <a href="http://www.cnitblog.com/yemoo/archive/2006/07/02/13151.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JS正则表达式提取字符串中所有汉字</title><link>http://www.cnitblog.com/yemoo/archive/2006/06/28/13002.html</link><dc:creator>Yemoo'S JS Blog</dc:creator><author>Yemoo'S JS Blog</author><pubDate>Wed, 28 Jun 2006 10:14:00 GMT</pubDate><guid>http://www.cnitblog.com/yemoo/archive/2006/06/28/13002.html</guid><wfw:comment>http://www.cnitblog.com/yemoo/comments/13002.html</wfw:comment><comments>http://www.cnitblog.com/yemoo/archive/2006/06/28/13002.html#Feedback</comments><slash:comments>3</slash:comments><wfw:commentRss>http://www.cnitblog.com/yemoo/comments/commentRss/13002.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/yemoo/services/trackbacks/13002.html</trackback:ping><description><![CDATA[  <script type="text/javascript" src="http://www.cnitblog.com/Files/yemoo/gg1.js"></script><p>再网上发现有人用vbscript正则表达式实现了这个功能，但代码很厂，偶改成js的了，很短的一段代码：</p>
  <textarea style="WIDTH: 99%; HEIGHT: 72px" rows="1">&lt;script type="text/javascript"&gt; 
var str="怎样从一个Html页面中提取所有汉字呢？不能有其它Html代码。";
alert(str.replace(/[^\u4e00-\u9fa5]/gi,""));
&lt;/script&gt;</textarea>
  <br />这里的关键是<font color="#ff0000">汉字escape后的编码范围是\u4e00-\u9fa5</font>，知道这个问题就好解决了。 
<p><script type="text/javascript" src="http://www.cnitblog.com/Files/yemoo/gg2.js"></script><img src ="http://www.cnitblog.com/yemoo/aggbug/13002.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/yemoo/" target="_blank">Yemoo'S JS Blog</a> 2006-06-28 18:14 <a href="http://www.cnitblog.com/yemoo/archive/2006/06/28/13002.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>匹配IP的正则表达式</title><link>http://www.cnitblog.com/yemoo/archive/2006/06/23/12774.html</link><dc:creator>Yemoo'S JS Blog</dc:creator><author>Yemoo'S JS Blog</author><pubDate>Fri, 23 Jun 2006 14:11:00 GMT</pubDate><guid>http://www.cnitblog.com/yemoo/archive/2006/06/23/12774.html</guid><wfw:comment>http://www.cnitblog.com/yemoo/comments/12774.html</wfw:comment><comments>http://www.cnitblog.com/yemoo/archive/2006/06/23/12774.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/yemoo/comments/commentRss/12774.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/yemoo/services/trackbacks/12774.html</trackback:ping><description><![CDATA[<script type="text/javascript" src="http://www.cnitblog.com/Files/yemoo/gg1.js"></script><br><br>以前以为用纯正则无法写出ip的匹配式。今天再一个正则表达式的站点看到一种方法虽然比较难理解，不过的确可以正确匹配IP.<br>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">^</span><span style="COLOR: #000000">((</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">]\d</span><span style="COLOR: #000000">|</span><span style="COLOR: #000000">25</span><span style="COLOR: #000000">[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">|</span><span style="COLOR: #000000">[</span><span style="COLOR: #000000">01</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">\d\d</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">)\.){</span><span style="COLOR: #000000">3</span><span style="COLOR: #000000">}(</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">4</span><span style="COLOR: #000000">]\d</span><span style="COLOR: #000000">|</span><span style="COLOR: #000000">25</span><span style="COLOR: #000000">[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">-</span><span style="COLOR: #000000">5</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">|</span><span style="COLOR: #000000">[</span><span style="COLOR: #000000">01</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">\d\d</span><span style="COLOR: #000000">?</span><span style="COLOR: #000000">)$</span></div>
主要把IP每段数字分成3部分来匹配，第一段：0-199.第二段：200-249 第三段：250-255<br>这样这个正则式子就好理解了。 <p><script type="text/javascript" src="http://www.cnitblog.com/Files/yemoo/gg2.js"></script><img src ="http://www.cnitblog.com/yemoo/aggbug/12774.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/yemoo/" target="_blank">Yemoo'S JS Blog</a> 2006-06-23 22:11 <a href="http://www.cnitblog.com/yemoo/archive/2006/06/23/12774.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>关于正则表达式的贪婪与非贪婪模式</title><link>http://www.cnitblog.com/yemoo/archive/2006/06/22/12675.html</link><dc:creator>Yemoo'S JS Blog</dc:creator><author>Yemoo'S JS Blog</author><pubDate>Thu, 22 Jun 2006 04:12:00 GMT</pubDate><guid>http://www.cnitblog.com/yemoo/archive/2006/06/22/12675.html</guid><wfw:comment>http://www.cnitblog.com/yemoo/comments/12675.html</wfw:comment><comments>http://www.cnitblog.com/yemoo/archive/2006/06/22/12675.html#Feedback</comments><slash:comments>9</slash:comments><wfw:commentRss>http://www.cnitblog.com/yemoo/comments/commentRss/12675.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/yemoo/services/trackbacks/12675.html</trackback:ping><description><![CDATA[<script type="text/javascript" src="http://www.cnitblog.com/Files/yemoo/gg1.js"></script><br><br>以前看正则表达式，但没有注意到正则表达式的贪婪与非贪婪模式，今天在经典上看到了这么段代码：<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">script</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br /></span><span style="COLOR: #0000ff">try</span><span style="COLOR: #000000">{<br />str</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">&lt;p&gt;abcdefg&lt;/p&gt;&lt;p&gt;abcdefghijkl&lt;/p&gt;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">;<br /><br />re1</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">str.match(</span><span style="COLOR: #000000">/&lt;</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">[\W\w]</span><span style="COLOR: #000000">+?&lt;</span><span style="COLOR: #000000">\</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">&gt;/</span><span style="COLOR: #000000">ig);<br />alert(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">非贪婪模式:\r\n\r\n１：</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">re1[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">\r\n２：</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">re1[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]);<br /><br />re1</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">str.match(</span><span style="COLOR: #000000">/&lt;</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">[\W\w]</span><span style="COLOR: #000000">+&lt;</span><span style="COLOR: #000000">\</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">&gt;/</span><span style="COLOR: #000000">ig);<br />alert(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">贪婪模式:\r\n\r\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">re1);<br /><br />re1</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">str.match(</span><span style="COLOR: #000000">/&lt;</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">(.</span><span style="COLOR: #000000">+?</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">\</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">&gt;/</span><span style="COLOR: #000000">i);<br />alert(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">非贪婪模式，且不要标记:\r\n\r\n１：</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">re1[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]);<br /><br />re1</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">str.match(</span><span style="COLOR: #000000">/&lt;</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000">(.</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">)</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">\</span><span style="COLOR: #000000">/</span><span style="COLOR: #000000">p</span><span style="COLOR: #000000">&gt;/</span><span style="COLOR: #000000">i);<br />alert(</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">贪婪模式，且不要标记:\r\n\r\n</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">re1[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]);<br />}</span><span style="COLOR: #0000ff">catch</span><span style="COLOR: #000000">(e){alert(e.description)}<br /></span><span style="COLOR: #000000">&lt;/</span><span style="COLOR: #000000">script</span><span style="COLOR: #000000">&gt;</span></div><br /><p><b><br />&#160;匹配次数中的贪婪与非贪婪</b></p><p>&#160;&#160;&#160; 在使用修饰匹配次数的特殊符号时，有几种表示方法可以使同一个表达式能够匹配不同的次数，比如："{m,n}", "{m,}", "?", "*", "+"，具体匹配的次数随被匹配的字符串而定。这种重复匹配不定次数的表达式在匹配过程中，总是尽可能多的匹配。比如，针对文本 "dxxxdxxxd"，举例如下：</p><table style="BORDER-COLLAPSE: collapse" cellspacing="0" cellpadding="3" bgcolor="#f8f8f8" border="1"><tbody><tr bgcolor="#f0f0f0"><td width="93"><p>表达式</p></td><td><p>匹配结果</p></td></tr><tr><td><p><a href="http://www.regexlab.com/zh/workshop.asp?pat=(d)(%5Cw%2B)&amp;txt=dxxxdxxxd">(d)(\w+)</a></p></td><td><p>"\w+" 将匹配第一个 "d" 之后的所有字符 "xxxdxxxd"</p></td></tr><tr><td><p><a href="http://www.regexlab.com/zh/workshop.asp?pat=(d)(%5Cw%2B)(d)&amp;txt=dxxxdxxxd">(d)(\w+)(d)</a></p></td><td><p>"\w+" 将匹配第一个 "d" 和最后一个 "d" 之间的所有字符 "xxxdxxx"。虽然 "\w+" 也能够匹配上最后一个 "d"，但是为了使整个表达式匹配成功，"\w+" 可以 "让出" 它本来能够匹配的最后一个 "d"</p></td></tr></tbody></table><p>&#160;&#160;&#160; 由此可见，"\w+" 在匹配的时候，总是尽可能多的匹配符合它规则的字符。虽然第二个举例中，它没有匹配最后一个 "d"，但那也是为了让整个表达式能够匹配成功。同理，带 "*" 和 "{m,n}" 的表达式都是尽可能地多匹配，带 "?" 的表达式在可匹配可不匹配的时候，也是尽可能的 "要匹配"。这 种匹配原则就叫作 "贪婪" 模式 。</p><p>&#160;&#160;&#160; 非贪婪模式：<br /><br />&#160;&#160;&#160; 在修饰匹配次数的特殊符号后再加上一个 "?" 号，则可以使匹配次数不定的表达式尽可能少的匹配，使可匹配可不匹配的表达式，尽可能的 "不匹配"。这种匹配原则叫作 "非贪婪" 模式，也叫作 "勉强" 模式。如果少匹配就会导致整个表达式匹配失败的时候，与贪婪模式类似，非贪婪模式会最小限度的再匹配一些，以使整个表达式匹配成功。举例如下，针对文本 "dxxxdxxxd" 举例：</p><table style="BORDER-COLLAPSE: collapse" cellspacing="0" cellpadding="3" bgcolor="#f8f8f8" border="1"><tbody><tr bgcolor="#f0f0f0"><td width="93"><p>表达式</p></td><td><p>匹配结果</p></td></tr><tr><td><p><a href="http://www.regexlab.com/zh/workshop.asp?pat=(d)(%5Cw%2B%3F)&amp;txt=dxxxdxxxd">(d)(\w+?)</a></p></td><td><p>"\w+?" 将尽可能少的匹配第一个 "d" 之后的字符，结果是："\w+?" 只匹配了一个 "x"</p></td></tr><tr><td><p><a href="http://www.regexlab.com/zh/workshop.asp?pat=(d)(%5Cw%2B%3F)(d)&amp;txt=dxxxdxxxd">(d)(\w+?)(d)</a></p></td><td><p>为了让整个表达式匹配成功，"\w+?" 不得不匹配 "xxx" 才可以让后边的 "d" 匹配，从而使整个表达式匹配成功。因此，结果是："\w+?" 匹配 "xxx"</p></td></tr></tbody></table><p>&#160;&#160;&#160; 更多的情况，举例如下：<br /><br />&#160;&#160;&#160; <a href="http://www.regexlab.com/zh/workshop.asp?pat=%3Ctd%3E%28%2E%2A%29%3C%2Ftd%3E&amp;txt=%3Ctd%3E%3Cp%3Eaa%3C%2Fp%3E%3C%2Ftd%3E%3Ctd%3E%3Cp%3Ebb%3C%2Fp%3E%3C%2Ftd%3E">举例1：表达式 "&lt;td&gt;(.*)&lt;/td&gt;" 与字符串 "&lt;td&gt;&lt;p&gt;aa&lt;/p&gt;&lt;/td&gt; &lt;td&gt;&lt;p&gt;bb&lt;/p&gt;&lt;/td&gt;" 匹配时</a>，匹配的结果是：成功；匹配到的内容是 "&lt;td&gt;&lt;p&gt;aa&lt;/p&gt;&lt;/td&gt; &lt;td&gt;&lt;p&gt;bb&lt;/p&gt;&lt;/td&gt;" 整个字符串， 表达式中的 "&lt;/td&gt;" 将与字符串中最后一个 "&lt;/td&gt;" 匹配。 <br /><br />&#160;&#160;&#160; <a href="http://www.regexlab.com/zh/workshop.asp?pat=%3Ctd%3E%28%2E%2A%3F%29%3C%2Ftd%3E&amp;txt=%3Ctd%3E%3Cp%3Eaa%3C%2Fp%3E%3C%2Ftd%3E%3Ctd%3E%3Cp%3Ebb%3C%2Fp%3E%3C%2Ftd%3E">举例2：相比之下，表达式 "&lt;td&gt;(.*?)&lt;/td&gt;" 匹配举例1中同样的字符串时</a>，将只得到 "&lt;td&gt;&lt;p&gt;aa&lt;/p&gt;&lt;/td&gt;"， 再次匹配下一个时，可以得到第二个 "&lt;td&gt;&lt;p&gt;bb&lt;/p&gt;&lt;/td&gt;"。</p><hr color="#fea089" size="1" /> <p><script type="text/javascript" src="http://www.cnitblog.com/Files/yemoo/gg2.js"></script><img src ="http://www.cnitblog.com/yemoo/aggbug/12675.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/yemoo/" target="_blank">Yemoo'S JS Blog</a> 2006-06-22 12:12 <a href="http://www.cnitblog.com/yemoo/archive/2006/06/22/12675.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JS正则表达式详解[摘自loemedia.com]</title><link>http://www.cnitblog.com/yemoo/archive/2006/06/17/12424.html</link><dc:creator>Yemoo'S JS Blog</dc:creator><author>Yemoo'S JS Blog</author><pubDate>Sat, 17 Jun 2006 09:20:00 GMT</pubDate><guid>http://www.cnitblog.com/yemoo/archive/2006/06/17/12424.html</guid><wfw:comment>http://www.cnitblog.com/yemoo/comments/12424.html</wfw:comment><comments>http://www.cnitblog.com/yemoo/archive/2006/06/17/12424.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cnitblog.com/yemoo/comments/commentRss/12424.html</wfw:commentRss><trackback:ping>http://www.cnitblog.com/yemoo/services/trackbacks/12424.html</trackback:ping><description><![CDATA[<script type="text/javascript" src="http://www.cnitblog.com/Files/yemoo/gg1.js"></script>  <p>JS的正则表达式</p>
  <p>//校验是否全由数字组成<br />function isDigit(s)<br />{<br />var patrn=/^[0-9]{1,20}$/;<br />if (!patrn.exec(s)) return false<br />return true<br />}</p>
  <p>//校验登录名：只能输入5-20个以字母开头、可带数字、&#8220;_&#8221;、&#8220;.&#8221;的字串<br />function isRegisterUserName(s)<br />{<br />var patrn=/^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}$/;<br />if (!patrn.exec(s)) return false<br />return true<br />}</p>
  <p>//校验用户姓名：只能输入1-30个以字母开头的字串<br />function isTrueName(s)<br />{<br />var patrn=/^[a-zA-Z]{1,30}$/;<br />if (!patrn.exec(s)) return false<br />return true<br />}</p>
  <p>//校验密码：只能输入6-20个字母、数字、下划线<br />function isPasswd(s)<br />{<br />var patrn=/^(\w){6,20}$/;<br />if (!patrn.exec(s)) return false<br />return true<br />}</p>
  <p>//校验普通电话、传真号码：可以&#8220;+&#8221;开头，除数字外，可含有&#8220;-&#8221;<br />function isTel(s)<br />{<br />//var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?(\d){1,12})+$/;<br />var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;<br />if (!patrn.exec(s)) return false<br />return true<br />}</p>
  <p>//校验手机号码：必须以数字开头，除数字外，可含有&#8220;-&#8221;<br />function isMobil(s)<br />{<br />var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;<br />if (!patrn.exec(s)) return false<br />return true<br />}</p>
  <p>//校验邮政编码<br />function isPostalCode(s)<br />{<br />//var patrn=/^[a-zA-Z0-9]{3,12}$/;<br />var patrn=/^[a-zA-Z0-9 ]{3,12}$/;<br />if (!patrn.exec(s)) return false<br />return true<br />}</p>
  <p>//校验搜索关键字<br />function isSearch(s)<br />{<br />var patrn=/^[^`~!@#$%^&amp;*()+=|\\\][\]\{\}:;'\,.&lt;&gt;/?]{1}[^`~!@$%^&amp;()+=|\\\][\]\{\}:;'\,.&lt;&gt;?]{0,19}$/;<br />if (!patrn.exec(s)) return false<br />return true<br />}</p>
  <p>function isIP(s) //by zergling<br />{<br />var patrn=/^[0-9.]{1,20}$/;<br />if (!patrn.exec(s)) return false<br />return true<br />}</p>
  <p>
    <br />正则表达式<br />"^\\d+$"　　//非负整数（正整数 + 0）<br />"^[0-9]*[1-9][0-9]*$"　　//正整数 <br />"^((-\\d+)|(0+))$"　　//非正整数（负整数 + 0） <br />"^-[0-9]*[1-9][0-9]*$"　　//负整数 <br />"^-?\\d+$"　　　　//整数 <br />"^\\d+(<a href="file://.//d+)?$">\\.\\d+)?$</a>"　　//非负浮点数（正浮点数 + 0） <br />"^(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*))$"　　//正浮点数 <br />"^((-\\d+(<a href="file://.//d+)?)%7C(0+(//.0+)?))$">\\.\\d+)?)|(0+(\\.0+)?))$</a>"　　//非正浮点数（负浮点数 + 0） <br />"^(-(([0-9]+\\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\\.[0-9]+)|([0-9]*[1-9][0-9]*)))$"　　//负浮点数 <br />"^(-?\\d+)(<a href="file://.//d+)?$">\\.\\d+)?$</a>"　　//浮点数 <br />"^[A-Za-z]+$"　　//由26个英文字母组成的字符串 <br />"^[A-Z]+$"　　//由26个英文字母的大写组成的字符串 <br />"^[a-z]+$"　　//由26个英文字母的小写组成的字符串 <br />"^[A-Za-z0-9]+$"　　//由数字和26个英文字母组成的字符串 <br />"^\\w+$"　　//由数字、26个英文字母或者下划线组成的字符串 <br />"^[\\w-]+(<a href="file://.%5B//w-%5D+)*@%5B//w-%5D+(//.%5B//w-%5D+)+$">\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$</a>"　　　　//email地址 <br />"^[a-zA-z]+://(<a href="file://w+(-//w+)*)(//.(//w+(-//w+)*))*(//?//S*)?$">\\w+(-\\w+)*)(\\.(\\w+(-\\w+)*))*(\\?\\S*)?$</a>"　　//url</p>
  <p>"^[A-Za-z0-9_]*$"</p>
  <p>
    <br />正则表达式使用详解</p>
  <p>简介 <br />简单的说，正则表达式是一种可以用于模式匹配和替换的强有力的工具。其作用如下：<br />测试字符串的某个模式。例如，可以对一个输入字符串进行测试，看在该字符串是否存在一个电话号码模式或一个信用卡号码模式。这称为数据有效性验证。 <br />替换文本。可以在文档中使用一个正则表达式来标识特定文字，然后可以全部将其删除，或者替换为别的文字。 <br />根据模式匹配从字符串中提取一个子字符串。可以用来在文本或输入字段中查找特定文字。 </p>
  <p>基本语法 <br />在对正则表达式的功能和作用有了初步的了解之后，我们就来具体看一下正则表达式的语法格式。 <br />正则表达式的形式一般如下：　　<br />/love/　　其中位于&#8220;/&#8221;定界符之间的部分就是将要在目标对象中进行匹配的模式。用户只要把希望查找匹配对象的模式内容放入&#8220;/&#8221;定界符之间即可。为了能够使用户更加灵活的定制模式内容，正则表达式提供了专门的&#8220;元字符&#8221;。所谓元字符就是指那些在正则表达式中具有特殊意义的专用字符，可以用来规定其前导字符（即位于元字符前面的字符）在目标对象中的出现模式。 <br />较为常用的元字符包括： &#8220;+&#8221;， &#8220;*&#8221;，以及 &#8220;?&#8221;。<br />&#8220;+&#8221;元字符规定其前导字符必须在目标对象中连续出现一次或多次。<br />&#8220;*&#8221;元字符规定其前导字符必须在目标对象中出现零次或连续多次。<br />&#8220;?&#8221;元字符规定其前导对象必须在目标对象中连续出现零次或一次。<br />下面，就让我们来看一下正则表达式元字符的具体应用。 <br />/fo+/　　因为上述正则表达式中包含&#8220;+&#8221;元字符，表示可以与目标对象中的 &#8220;fool&#8221;, &#8220;fo&#8221;, 或者 &#8220;football&#8221;等在字母f后面连续出现一个或多个字母o的字符串相匹配。 <br />/eg*/　　因为上述正则表达式中包含&#8220;*&#8221;元字符，表示可以与目标对象中的 &#8220;easy&#8221;, &#8220;ego&#8221;, 或者 &#8220;egg&#8221;等在字母e后面连续出现零个或多个字母g的字符串相匹配。 <br />/Wil?/　　因为上述正则表达式中包含&#8220;？&#8221;元字符，表示可以与目标对象中的 &#8220;Win&#8221;, 或者&#8220;Wilson&#8221;,等在字母i后面连续出现零个或一个字母l的字符串相匹配。 <br />有时候不知道要匹配多少字符。为了能适应这种不确定性，正则表达式支持限定符的概念。这些限定符可以指定正则表达式的一个给定组件必须要出现多少次才能满足匹配。<br />{n} n 是一个非负整数。匹配确定的 n 次。例如，'o{2}' 不能匹配 "Bob" 中的 'o'，但是能匹配 "food" 中的两个 o。<br />{n,} n 是一个非负整数。至少匹配 n 次。例如，'o{2,}' 不能匹配 "Bob" 中的 'o'，但能匹配 "foooood" 中的所有 o。'o{1,}' 等价于 'o+'。'o{0,}' 则等价于 'o*'。<br />{n,m} m 和 n 均为非负整数，其中n &lt;= m。最少匹配 n 次且最多匹配 m 次。例如，"o{1,3}" 将匹配 "fooooood" 中的前三个 o。'o{0,1}' 等价于 'o?'。请注意在逗号和两个数之间不能有空格。</p>
  <p>除了元字符之外，用户还可以精确指定模式在匹配对象中出现的频率。例如，/jim {2,6}/ 上述正则表达式规定字符m可以在匹配对象中连续出现2-6次，因此，上述正则表达式可以同jimmy或jimmmmmy等字符串相匹配。 <br />在对如何使用正则表达式有了初步了解之后，我们来看一下其它几个重要的元字符的使用方式。 <br />\s：用于匹配单个空格符，包括tab键和换行符； <br />\S：用于匹配除单个空格符之外的所有字符； <br />\d：用于匹配从0到9的数字； <br />\w：用于匹配字母，数字或下划线字符； <br />\W：用于匹配所有与\w不匹配的字符； <br />. ：用于匹配除换行符之外的所有字符。 <br />（说明：我们可以把\s和\S以及\w和\W看作互为逆运算） <br />下面，我们就通过实例看一下如何在正则表达式中使用上述元字符。 <br />/\s+/ 上述正则表达式可以用于匹配目标对象中的一个或多个空格字符。 <br />/\d000/　如果我们手中有一份复杂的财务报表，那么我们可以通过上述正则表达式轻而易举的查找到所有总额达千元的款项。 <br />除了我们以上所介绍的元字符之外，正则表达式中还具有另外一种较为独特的专用字符，即定位符。定位符用于规定匹配模式在目标对象中的出现位置。 较为常用的定位符包括： &#8220;^&#8221;, &#8220;$&#8221;, &#8220;\b&#8221; 以及 &#8220;\B&#8221;。<br />&#8220;^&#8221;定位符规定匹配模式必须出现在目标字符串的开头<br />&#8220;$&#8221;定位符规定匹配模式必须出现在目标对象的结尾<br />&#8220;\b&#8221;定位符规定匹配模式必须出现在目标字符串的开头或结尾的两个边界之一<br />&#8220;\B&#8221;定位符则规定匹配对象必须位于目标字符串的开头和结尾两个边界之内，即匹配对象既不能作为目标字符串的开头，也不能作为目标字符串的结尾。同样，我们也可以把&#8220;^&#8221;和&#8220;$&#8221;以及&#8220;\b&#8221;和&#8220;\B&#8221;看作是互为逆运算的两组定位符。举例来说： /^hell/　因为上述正则表达式中包含&#8220;^&#8221;定位符，所以可以与目标对象中以 &#8220;hell&#8221;, &#8220;hello&#8221;或&#8220;hellhound&#8221;开头的字符串相匹配。 /ar$/　因为上述正则表达式中包含&#8220;$&#8221;定位符，所以可以与目标对象中以 &#8220;car&#8221;, &#8220;bar&#8221;或 &#8220;ar&#8221; 结尾的字符串相匹配。 /\bbom/　因为上述正则表达式模式以&#8220;\b&#8221;定位符开头，所以可以与目标对象中以 &#8220;bomb&#8221;, 或 &#8220;bom&#8221;开头的字符串相匹配。/man\b/　因为上述正则表达式模式以&#8220;\b&#8221;定位符结尾，所以可以与目标对象中以 &#8220;human&#8221;, &#8220;woman&#8221;或 &#8220;man&#8221;结尾的字符串相匹配。 <br />为了能够方便用户更加灵活的设定匹配模式，正则表达式允许使用者在匹配模式中指定某一个范围而不局限于具体的字符。例如： <br />/[A-Z]/　　上述正则表达式将会与从A到Z范围内任何一个大写字母相匹配。<br />/[a-z]/　　上述正则表达式将会与从a到z范围内任何一个小写字母相匹配。 <br />/[0-9]/ 　上述正则表达式将会与从0到9范围内任何一个数字相匹配。 <br />/([a-z][A-Z][0-9])+/　　上述正则表达式将会与任何由字母和数字组成的字符串，如 &#8220;aB0&#8221; 等相匹配。这里需要提醒用户注意的一点就是可以在正则表达式中使用 &#8220;()&#8221; 把字符串组合在一起。&#8220;()&#8221;符号包含的内容必须同时出现在目标对象中。因此，上述正则表达式将无法与诸如 &#8220;abc&#8221;等的字符串匹配，因为&#8220;abc&#8221;中的最后一个字符为字母而非数字。 <br />如果我们希望在正则表达式中实现类似编程逻辑中的&#8220;或&#8221;运算，在多个不同的模式中任选一个进行匹配的话，可以使用管道符 &#8220;|&#8221;。例如：/to|too|2/　上述正则表达式将会与目标对象中的 &#8220;to&#8221;, &#8220;too&#8221;, 或 &#8220;2&#8221; 相匹配。<br />正则表达式中还有一个较为常用的运算符，即否定符 &#8220;[^]&#8221;。与我们前文所介绍的定位符 &#8220;^&#8221; 不同，否定符 &#8220;[^]&#8221;规定目标对象中不能存在模式中所规定的字符串。例如：/[^A-C]/　上述字符串将会与目标对象中除A，B，和C之外的任何字符相匹配。一般来说，当&#8220;^&#8221;出现在 &#8220;[]&#8221;内时就被视做否定运算符；而当&#8220;^&#8221;位于&#8220;[]&#8221;之外，或没有&#8220;[]&#8221;时，则应当被视做定位符。<br />最后，当用户需要在正则表达式的模式中加入元字符，并查找其匹配对象时，可以使用转义符&#8220;\&#8221;。例如：/Th\*/ 　上述正则表达式将会与目标对象中的&#8220;Th*&#8221;而非&#8220;The&#8221;等相匹配。<br />在构造正则表达式之后，就可以象数学表达式一样来求值，也就是说，可以从左至右并按照一个优先级顺序来求值。优先级如下：<br />1．\ 转义符<br />2．(), (?:), (?=), [] 圆括号和方括号<br />3．*, +, ?, {n}, {n,}, {n,m} 限定符<br />4．^, $, \anymetacharacter 位置和顺序<br />5．|&#8220;或&#8221;操作</p>
  <p>
    <br />使用实例 <br />在JavaScript 1.2中带有一个功能强大的RegExp()对象，可以用来进行正则表达式的匹配操作。其中的test()方法可以检验目标对象中是否包含匹配模式，并相应的返回true或false。<br />我们可以使用JavaScript编写以下脚本，验证用户输入的邮件地址的有效性。<br />-------------------------------------------------------- <br />&lt;html&gt; <br />&lt;head&gt; <br />　 &lt;script language="Javascript1.2"&gt; <br />　　　　 &lt;!-- start hiding <br />　　　　 function verifyAddress(obj) <br />　　　　　{ <br />　　　　　　var email = obj.email.value; <br />　　　　　　var pattern = <br />/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/; <br />　　　　　　flag = pattern.test(email); <br />　　　　　　if(flag) <br />　　　　　　{ <br />　　　　　　　alert(&#8220;Your email address is correct!&#8221;); <br />　　　　　　　return true; <br />　　　　　　} <br />　　　　　　else <br />　　　　　　　{ <br />　　　　　　　　alert(&#8220;Please try again!&#8221;); <br />　　　　　　　　return false; <br />　　　　　　　 } <br />　　　　　 } <br />　　　　 // stop hiding --&gt; <br />　　　 &lt;/script&gt; <br />　　&lt;/head&gt; <br />　 &lt;body&gt; <br />　　 &lt;form onSubmit="return verifyAddress(this);"&gt; <br />　　　 &lt;input name="email" type="text"&gt; <br />　　　 &lt;input type="submit"&gt; <br />　　　 &lt;/form&gt; <br />　　&lt;/body&gt; <br />&lt;/html&gt; <br />-------------------------------------------------------- </p>
  <p>正则表达式对象<br />本对象包含正则表达式模式以及表明如何应用模式的标志。<br />语法 1 re = /pattern/[flags]<br />语法 2 re = new RegExp("pattern",["flags"]) <br />参数<br />re<br />必选项。将要赋值为正则表达式模式的变量名。 <br />Pattern<br />必选项。要使用的正则表达式模式。如果使用语法 1，用 "/" 字符分隔模式。如果用语法 2，用引号将模式引起来。 </p>
  <p>Flags <br />可选项。如果使用语法 2 要用引号将 flag 引起来。标志可以组合使用，可用的有： <br />g （全文查找出现的所有 pattern） <br />i （忽略大小写） <br />m （多行查找） <br />示例<br />下面的示例创建一个包含正则表达式模式及相关标志的对象(re)，向您演示正则表达式对象的用法。在本例中，作为结果的正则表达式对象又用于 match 方法中：<br />function MatchDemo()<br />{<br />var r, re; // 声明变量。<br />var s = "The rain in Spain falls mainly in the plain";<br />re = new RegExp("ain","g"); // 创建正则表达式对象。<br />r = s.match(re); // 在字符串 s 中查找匹配。<br />return(r); <br />}<br />返回值： ain,ain,ain,ain<br />属性 lastIndex 属性 | source 属性<br />方法 compile 方法 | exec 方法 | test 方法<br />要求 版本 3<br />请参阅 RegExp 对象 | 正则表达式语法 | String 对象</p>
  <p>exec 方法<br />用正则表达式模式在字符串中运行查找，并返回包含该查找结果的一个数组。<br />rgExp.exec(str)<br />参数<br />rgExp <br />必选项。包含正则表达式模式和可用标志的正则表达式对象。 <br />str <br />必选项。要在其中执行查找的 String 对象或字符串文字。 <br />说明<br />如果 exec 方法没有找到匹配，则它返回 null。如果它找到匹配，则 exec 方法返回一个数组，并且更新全局 RegExp 对象的属性，以反映匹配结果。数组的0元素包含了完整的匹配，而第1到n元素中包含的是匹配中出现的任意一个子匹配。这相当于没有设置全局标志 (g) 的 match 方法。<br />如果为正则表达式设置了全局标志，exec 从以 lastIndex 的值指示的位置开始查找。如果没有设置全局标志，exec 忽略 lastIndex 的值，从字符串的起始位置开始搜索。<br />exec 方法返回的数组有三个属性，分别是 input、index 和 lastIndex。Input 属性包含了整个被查找的字符串。Index 属性中包含了整个被查找字符串中被匹配的子字符串的位置。LastIndex 属性中包含了匹配中最后一个字符的下一个位置。<br />示例<br />下面的例子举例说明了 exec 方法的用法：<br />function RegExpTest()<br />{<br />var ver = Number(ScriptEngineMajorVersion() + "." + ScriptEngineMinorVersion())<br />if (ver &gt;= 5.5){ // 测试 JScript 的版本。<br />var src = "The rain in Spain falls mainly in the plain.";<br />var re = /\w+/g; // 创建正则表达式模式。<br />var arr;<br />while ((arr = re.exec(src)) != null)<br />document.write(arr.index + "-" + arr.lastIndex + arr + "\t");<br />}<br />else{<br />alert("请使用 JScript 的更新版本");<br />}<br />}<br />返回值：0-3The 4-8rain 9-11in 12-17Spain 18-23falls 24-30mainly 31-33in 34-37the 38-43plain<br />test 方法<br />返回一个 Boolean 值，它指出在被查找的字符串中是否存在模式。<br />rgexp.test(str) <br />参数<br />rgexp<br />必选项。包含正则表达式模式或可用标志的正则表达式对象。 <br />str <br />必选项。要在其上测试查找的字符串。 <br />说明<br />test 方法检查在字符串中是否存在一个模式，如果存在则返回 true，否则就返回 false。<br />全局 RegExp 对象的属性不由 test 方法来修改。<br />示例<br />下面的例子举例说明了 test 方法的用法：<br />function TestDemo(re, s)<br />{<br />var s1; // 声明变量。<br />// 检查字符串是否存在正则表达式。<br />if (re.test(s)) // 测试是否存在。<br />s1 = " contains "; // s 包含模式。<br />else<br />s1 = " does not contain "; // s 不包含模式。<br />return("'" + s + "'" + s1 + "'"+ re.source + "'"); // 返回字符串。<br />}<br />函数调用：document.write (TestDemo(/ain+/ ,"The rain in Spain falls mainly in the plain."));<br />返回值：'The rain in Spain falls mainly in the plain.' contains 'ain+'<br />match 方法<br />使用正则表达式模式对字符串执行查找，并将包含查找的结果作为数组返回。<br />stringObj.match(rgExp) <br />参数<br />stringObj <br />必选项。对其进行查找的 String 对象或字符串文字。 <br />rgExp <br />必选项。为包含正则表达式模式和可用标志的正则表达式对象。也可以是包含正则表达式模式和可用标志的变量名或字符串文字。 <br />说明<br />如果 match 方法没有找到匹配，返回 null。如果找到匹配返回一个数组并且更新全局 RegExp 对象的属性以反映匹配结果。<br />match 方法返回的数组有三个属性：input、index 和 lastIndex。Input 属性包含整个的被查找字符串。Index 属性包含了在整个被查找字符串中匹配的子字符串的位置。LastIndex 属性包含了最后一次匹配中最后一个字符的下一个位置。<br />如果没有设置全局标志 (g)，数组的 0 元素包含整个匹配，而第 1 到 n 元素包含了匹配中曾出现过的任一个子匹配。这相当于没有设置全局标志的 exec 方法。如果设置了全局标志，元素 0 到 n 中包含所有匹配。<br />示例<br />下面的示例演示了match 方法的用法：<br />function MatchDemo()<br />{<br />var r, re; // 声明变量。<br />var s = "The rain in Spain falls mainly in the plain";<br />re = /ain/i; // 创建正则表达式模式。<br />r = s.match(re); // 尝试匹配搜索字符串。<br />return(r); // 返回第一次出现 "ain" 的地方。<br />}<br />返回值：ain<br />本示例说明带 g 标志设置的 match 方法的用法。<br />function MatchDemo()<br />{<br />var r, re; // 声明变量。<br />var s = "The rain in Spain falls mainly in the plain";<br />re = /ain/ig; // 创建正则表达式模式。<br />r = s.match(re); // 尝试去匹配搜索字符串。<br />return(r); // 返回的数组包含了所有 "ain" <br />// 出现的四个匹配。<br />}<br />返回值：ain,ain,ain,ain<br />上面几行代码演示了字符串文字的 match 方法的用法。<br />var r, re = "Spain";<br />r = "The rain in Spain".replace(re, "Canada");<br />return r;<br />返回值：The rain in Canada<br />search 方法<br />返回与正则表达式查找内容匹配的第一个子字符串的位置。<br />stringObj.search(rgExp)<br />参数<br />stringObj <br />必选项。要在其上进行查找的 String 对象或字符串文字。 <br />rgExp <br />必选项。包含正则表达式模式和可用标志的正则表达式对象。 <br />说明<br />search 方法指明是否存在相应的匹配。如果找到一个匹配，search 方法将返回一个整数值，指明这个匹配距离字符串开始的偏移位置。如果没有找到匹配，则返回 -1。<br />示例<br />下面的示例演示了 search 方法的用法。<br />function SearchDemo()<br />{<br />var r, re; // 声明变量。<br />var s = "The rain in Spain falls mainly in the plain.";<br />re = /falls/i; // 创建正则表达式模式。<br />r = s.search(re); // 查找字符串。<br />return(r); // 返回 Boolean 结果。<br />}<br />返回值：18</p>
  <p>
    <br />正则表达式语法<br />一个正则表达式就是由普通字符（例如字符 a 到 z）以及特殊字符（称为元字符）组成的文字模式。该模式描述在查找文字主体时待匹配的一个或多个字符串。正则表达式作为一个模板，将某个字符模式与所搜索的字符串进行匹配。</p>
  <p>这里有一些可能会遇到的正则表达式示例：</p>
  <p>JScript VBScript 匹配 <br />/^\[ \t]*$/ "^\[ \t]*$" 匹配一个空白行。 <br />/\d{2}-\d{5}/ "\d{2}-\d{5}" 验证一个ID 号码是否由一个2位数字，一个连字符以及一个5位数字组成。 <br />/&lt;(.*)&gt;.*&lt;\/\1&gt;/ "&lt;(.*)&gt;.*&lt;\/\1&gt;" 匹配一个 HTML 标记。 </p>
  <p>
    <br />下表是元字符及其在正则表达式上下文中的行为的一个完整列表：</p>
  <p>字符 描述 <br />\ 将下一个字符标记为一个特殊字符、或一个原义字符、或一个 后向引用、或一个八进制转义符。例如，'n' 匹配字符 "n"。'\n' 匹配一个换行符。序列 '\\' 匹配 "\" 而 "\(" 则匹配 "("。 <br />^ 匹配输入字符串的开始位置。如果设置了 RegExp 对象的 Multiline 属性，^ 也匹配 '\n' 或 '\r' 之后的位置。 <br />$ 匹配输入字符串的结束位置。如果设置了RegExp 对象的 Multiline 属性，$ 也匹配 '\n' 或 '\r' 之前的位置。 <br />* 匹配前面的子表达式零次或多次。例如，zo* 能匹配 "z" 以及 "zoo"。 * 等价于{0,}。 <br />+ 匹配前面的子表达式一次或多次。例如，'zo+' 能匹配 "zo" 以及 "zoo"，但不能匹配 "z"。+ 等价于 {1,}。 <br />? 匹配前面的子表达式零次或一次。例如，"do(es)?" 可以匹配 "do" 或 "does" 中的"do" 。? 等价于 {0,1}。 <br />{n} n 是一个非负整数。匹配确定的 n 次。例如，'o{2}' 不能匹配 "Bob" 中的 'o'，但是能匹配 "food" 中的两个 o。 <br />{n,} n 是一个非负整数。至少匹配n 次。例如，'o{2,}' 不能匹配 "Bob" 中的 'o'，但能匹配 "foooood" 中的所有 o。'o{1,}' 等价于 'o+'。'o{0,}' 则等价于 'o*'。 <br />{n,m} m 和 n 均为非负整数，其中n &lt;= m。最少匹配 n 次且最多匹配 m 次。刘， "o{1,3}" 将匹配 "fooooood" 中的前三个 o。'o{0,1}' 等价于 'o?'。请注意在逗号和两个数之间不能有空格。 <br />? 当该字符紧跟在任何一个其他限制符 (*, +, ?, {n}, {n,}, {n,m}) 后面时，匹配模式是非贪婪的。非贪婪模式尽可能少的匹配所搜索的字符串，而默认的贪婪模式则尽可能多的匹配所搜索的字符串。例如，对于字符串 "oooo"，'o+?' 将匹配单个 "o"，而 'o+' 将匹配所有 'o'。 <br />. 匹配除 "\n" 之外的任何单个字符。要匹配包括 '\n' 在内的任何字符，请使用象 '[.\n]' 的模式。 <br />(pattern) 匹配pattern 并获取这一匹配。所获取的匹配可以从产生的 Matches 集合得到，在VBScript 中使用 SubMatches 集合，在JScript 中则使用 $0&#8230;$9 属性。要匹配圆括号字符，请使用 '\(' 或 '\)'。 <br />(?:pattern) 匹配 pattern 但不获取匹配结果，也就是说这是一个非获取匹配，不进行存储供以后使用。这在使用 "或" 字符 (|) 来组合一个模式的各个部分是很有用。例如， 'industr(?:y|ies) 就是一个比 'industry|industries' 更简略的表达式。 <br />(?=pattern) 正向预查，在任何匹配 pattern 的字符串开始处匹配查找字符串。这是一个非获取匹配，也就是说，该匹配不需要获取供以后使用。例如， 'Windows (?=95|98|NT|2000)' 能匹配 "Windows 2000" 中的 "Windows" ，但不能匹配 "Windows 3.1" 中的 "Windows"。预查不消耗字符，也就是说，在一个匹配发生后，在最后一次匹配之后立即开始下一次匹配的搜索，而不是从包含预查的字符之后开始。 <br />(?!pattern) 负向预查，在任何不匹配Negative lookahead matches the search string at any point where a string not matching pattern 的字符串开始处匹配查找字符串。这是一个非获取匹配，也就是说，该匹配不需要获取供以后使用。例如'Windows (?!95|98|NT|2000)' 能匹配 "Windows 3.1" 中的 "Windows"，但不能匹配 "Windows 2000" 中的 "Windows"。预查不消耗字符，也就是说，在一个匹配发生后，在最后一次匹配之后立即开始下一次匹配的搜索，而不是从包含预查的字符之后开始 <br />x|y 匹配 x 或 y。例如，'z|food' 能匹配 "z" 或 "food"。'(z|f)ood' 则匹配 "zood" 或 "food"。 <br />[xyz] 字符集合。匹配所包含的任意一个字符。例如， '[abc]' 可以匹配 "plain" 中的 'a'。 <br />[^xyz] 负值字符集合。匹配未包含的任意字符。例如， '[^abc]' 可以匹配 "plain" 中的'p'。 <br />[a-z] 字符范围。匹配指定范围内的任意字符。例如，'[a-z]' 可以匹配 'a' 到 'z' 范围内的任意小写字母字符。 <br />[^a-z] 负值字符范围。匹配任何不在指定范围内的任意字符。例如，'[^a-z]' 可以匹配任何不在 'a' 到 'z' 范围内的任意字符。 <br />\b 匹配一个单词边界，也就是指单词和空格间的位置。例如， 'er\b' 可以匹配"never" 中的 'er'，但不能匹配 "verb" 中的 'er'。 <br />\B 匹配非单词边界。'er\B' 能匹配 "verb" 中的 'er'，但不能匹配 "never" 中的 'er'。 <br />\cx 匹配由x指明的控制字符。例如， \cM 匹配一个 Control-M 或回车符。 x 的值必须为 A-Z 或 a-z 之一。否则，将 c 视为一个原义的 'c' 字符。 <br />\d 匹配一个数字字符。等价于 [0-9]。 <br />\D 匹配一个非数字字符。等价于 [^0-9]。 <br />\f 匹配一个换页符。等价于 \x0c 和 \cL。 <br />\n 匹配一个换行符。等价于 \x0a 和 \cJ。 <br />\r 匹配一个回车符。等价于 \x0d 和 \cM。 <br />\s 匹配任何空白字符，包括空格、制表符、换页符等等。等价于 [ \f\n\r\t\v]。 <br />\S 匹配任何非空白字符。等价于 [^ \f\n\r\t\v]。 <br />\t 匹配一个制表符。等价于 \x09 和 \cI。 <br />\v 匹配一个垂直制表符。等价于 \x0b 和 \cK。 <br />\w 匹配包括下划线的任何单词字符。等价于'[A-Za-z0-9_]'。 <br />\W 匹配任何非单词字符。等价于 '[^A-Za-z0-9_]'。 <br />\xn 匹配 n，其中 n 为十六进制转义值。十六进制转义值必须为确定的两个数字长。例如， '\x41' 匹配 "A"。'\x041' 则等价于 '\x04' &amp; "1"。正则表达式中可以使用 ASCII 编码。. <br />\num 匹配 num，其中 num 是一个正整数。对所获取的匹配的引用。例如，'(.)\1' 匹配两个连续的相同字符。 <br />\n 标识一个八进制转义值或一个后向引用。如果 \n 之前至少 n 个获取的子表达式，则 n 为后向引用。否则，如果 n 为八进制数字 (0-7)，则 n 为一个八进制转义值。 <br />\nm 标识一个八进制转义值或一个后向引用。如果 \nm 之前至少有is preceded by at least nm 个获取得子表达式，则 nm 为后向引用。如果 \nm 之前至少有 n 个获取，则 n 为一个后跟文字 m 的后向引用。如果前面的条件都不满足，若 n 和 m 均为八进制数字 (0-7)，则 \nm 将匹配八进制转义值 nm。 <br />\nml 如果 n 为八进制数字 (0-3)，且 m 和 l 均为八进制数字 (0-7)，则匹配八进制转义值 nml。 <br />\un 匹配 n，其中 n 是一个用四个十六进制数字表示的 Unicode 字符。例如， \u00A9 匹配版权符号 (?)。 </p>
  <p>
    <br />优先权顺序<br />在构造正则表达式之后，就可以象数学表达式一样来求值，也就是说，可以从左至右并按照一个优先权顺序来求值。 </p>
  <p>下表从最高优先级到最低优先级列出各种正则表达式操作符的优先权顺序：</p>
  <p>操作符 描述 <br />\ 转义符 <br />(), (?:), (?=), [] 圆括号和方括号 <br />*, +, ?, {n}, {n,}, {n,m} 限定符 <br />^, $, \anymetacharacter 位置和顺序 <br />| &#8220;或&#8221;操作 </p>
  <p>
    <br />普通字符<br />普通字符由所有那些未显式指定为元字符的打印和非打印字符组成。这包括所有的大写和小写字母字符，所有数字，所有标点符号以及一些符号。 </p>
  <p>最简单的正则表达式是一个单独的普通字符，可以匹配所搜索字符串中的该字符本身。例如，单字符模式 'A' 可以匹配所搜索字符串中任何位置出现的字母 'A'。这里有一些单字符正则表达式模式的示例：</p>
  <p>/a/<br />/7/<br />/M/<br />等价的 VBScript 单字符正则表达式为：</p>
  <p>"a"<br />"7"<br />"M"<br />可以将多个单字符组合在一起得到一个较大的表达式。例如，下面的 JScript 正则表达式不是别的，就是通过组合单字符表达式 'a'、'7'以及 'M' 所创建出来的一个表达式。 </p>
  <p>/a7M/<br />等价的 VBScript 表达式为：</p>
  <p>"a7M"<br />请注意这里没有连接操作符。所需要做的就是将一个字符放在了另一个字符后面。</p><script type="text/javascript" src="http://www.cnitblog.com/Files/yemoo/gg2.js"></script>
 <img src ="http://www.cnitblog.com/yemoo/aggbug/12424.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cnitblog.com/yemoo/" target="_blank">Yemoo'S JS Blog</a> 2006-06-17 17:20 <a href="http://www.cnitblog.com/yemoo/archive/2006/06/17/12424.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>