<?xml version="1.0" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="css/rss.xslt"?>
<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备忘录 - 其他</title><link>http://www.dazix.cn/</link><description>沈阳网站制作|建设|策划|改版|SEO|优化|排名|推广|网络营销 - </description><generator>RainbowSoft Studio Z-Blog 1.8 Spirit Build 80722</generator><language>zh-CN</language><copyright>Copyright 2007-2010 www.dazix.cn All Rights Reserved. Powered By Z-Blog 专注于沈阳本地网站制作、建设、策划、改版、SEO、优化、排名、推广、网络营销、Archiver</copyright><pubDate>Sun, 05 Sep 2010 08:07:53 +0800</pubDate><item><title>改进ASP 的字符串处理性能</title><author>30538357@qq.com (dazix)</author><link>http://www.dazix.cn/post/67.html</link><pubDate>Fri, 26 Mar 2010 12:22:50 +0800</pubDate><guid>http://www.dazix.cn/post/67.html</guid><description><![CDATA[<h2 class="dtH1"><a name="aspstrcatn_intro"></a>简介</h2><p>编写 ASP 页面时，开发人员实际上是创建一个格式化的文本流，通过 ASP 提供的 <b>Response</b> 对象写入 Web 客户端。创建此文本流的方法有多种，而您选择的方法将对 Web 应用程序的性能和可缩放性产生很大影响。很多次，在我帮助客户优化其 Web 应用程序的性能时，发现其中一个比较有效的方法是更改 HTML 流的创建方式。本文将介绍几种常用技术，并测试它们对一个简单的 ASP 页面的性能所产生的影响。</p><h2 class="dtH1"><a name="aspstrcatn_design"></a>ASP 设计</h2><p>许多 ASP 开发人员都遵循良好的软件工程原则，尽可能地将其代码模块化。这种设计通常使用一些包含文件，这些文件中包含对页面的特定不连续部分进行格式化生成的函数。这些函数的字符串输出（通常是 HTML 表格代码）可以通过各种组合创建一个完整的页面。某些开发人员对此方法进行了改进，将这些 HTML 函数移到 Visual Basic COM 组件中，希望充分利用已编译的代码提供的额外性能。</p><p>尽管这种设计方法很不错，但创建组成这些不连续 HTML 代码组件的字符串所使用的方法将对 Web 站点的性能和可缩放性产生很大的影响，无论实际的操作是在 ASP 包含文件中执行还是在 Visual Basic COM 组件中执行。</p><h2 class="dtH1"><a name="aspstrcatn_concat"></a>字符串连接</h2><p>请看以下 <b>WriteHTML</b> 函数的代码片断。名为 Data 的参数只是一个字符串数组，其中包含一些要格式化为表格结构的数据（例如，从数据库返回的数据）。</p><pre class="code">Function WriteHTML( Data )Dim nRepFornRep = 0 to 99&nbsp;&nbsp;&nbsp;&nbsp;sHTML = sHTML &amp; vbcrlf    &amp; &quot;&lt;TR&gt;&lt;TD&gt;&quot; &amp; (nRep + 1) &amp; &quot;&lt;/TD&gt;&lt;TD&gt;&quot;    &amp; Data( 0, nRep ) &amp; &quot;&lt;/TD&gt;&lt;TD&gt;&quot;    &amp; Data( 1, nRep ) &amp; &quot;&lt;/TD&gt;&lt;TD&gt;&quot;    &amp; Data( 2, nRep ) &amp; &quot;&lt;/TD&gt;&lt;TD&gt;&quot;    &amp; Data( 3, nRep ) &amp; &quot;&lt;/TD&gt;&lt;TD&gt;&quot;    &amp; Data( 4, nRep ) &amp; &quot;&lt;/TD&gt;&lt;TD&gt;&quot;    &amp; Data( 5, nRep ) &amp; &quot;&lt;/TD&gt;&lt;/TR&gt;&quot;NextWriteHTML = sHTMLEnd Function</pre><p>这是很多 ASP 和 Visual Basic 开发人员创建 HTML 代码时常用的方法。sHTML 变量中包含的文本返回到调用代码，然后使用 <b>Response.Write</b> 写入客户端。当然，这还可以表示为直接嵌入不包含 <b>WriteHTML</b> 函数的页面的类似代码。此代码的问题是，ASP 和 Visual Basic 使用的字符串数据类型（BSTR 或 Basic 字符串）实际上无法更改长度。这意味着每当字符串长度更改时，内存中字符串的原始表示形式都将遭到破坏，而且将创建一个包含新字符串数据的新的表示形式：这将增加分配内存和解除分配内存的操作。当然，ASP 和 Visual Basic 已为您解决了这一问题，因此实际开销不会立即显现出来。分配内存和解除分配内存要求基本运行时代码解除各个专用锁定，因此需要大量开销。当字符串变得很大并且有大块内存要被快速连续地分配和解除分配时，此问题变得尤为明显，就像在大型字符串连接期间出现的情况一样。尽管这一问题对单用户环境的影响不大，但在服务器环境（例如，在 Web 服务器上运行的 ASP 应用程序）中，它将导致严重的性能和可缩放性问题。</p><p>下面，我们回到上述代码片段：此代码中要执行多少个字符串分配操作？答案是 16 个。在这种情况下，&ldquo;<code class="ce">&amp;</code>&rdquo;运算符的每次应用都将导致变量 <code class="ce">sHTML</code> 所指的字符串被破坏和重新创建。前面已经提到，字符串分配的开销很大，并且随着字符串的增大而增加，因此，我们可以对上述代码进行改进。</p><h2 class="dtH1"><a name="aspstrcatn_parens"></a>快捷的解决方案</h2><p>有两种方法可以缓解字符串连接的影响，第一种方法是尝试减小要处理的字符串的大小，第二种方法是尝试减少执行字符串分配操作的数目。请参见下面所示的 <b>WriteHTML</b> 代码的修订版本。</p><pre class="code">Function WriteHTML( Data )Dim nRepFornRep = 0 to 99    sHTML = sHTML &amp; ( vbcrlf    &amp; &quot;&lt;TR&gt;&lt;TD&gt;&quot; &amp; (nRep + 1) &amp; &quot;&lt;/TD&gt;&lt;TD&gt;&quot;    &amp; Data( 0, nRep ) &amp; &quot;&lt;/TD&gt;&lt;TD&gt;&quot;    &amp; Data( 1, nRep ) &amp; &quot;&lt;/TD&gt;&lt;TD&gt;&quot;    &amp; Data( 2, nRep ) &amp; &quot;&lt;/TD&gt;&lt;TD&gt;&quot;    &amp; Data( 3, nRep ) &amp; &quot;&lt;/TD&gt;&lt;TD&gt;&quot;    &amp; Data( 4, nRep ) &amp; &quot;&lt;/TD&gt;&lt;TD&gt;&quot;    &amp; Data( 5, nRep ) &amp; &quot;&lt;/TD&gt;&lt;/TR&gt;&quot;)NextWriteHTML = sHTMLEnd Function</pre><p>乍一看，可能很难发现这段代码与上一个代码示例的差别。其实，此代码只是在 <code class="ce">sHTML = sHTML &amp;</code> 后的内容外面加上了括号。这实际上是通过更改优先顺序，来减小大多数字符串连接操作中处理的字符串大小。在最初的代码示例中，ASP 编译器将查看等号右边的表达式，并从左到右进行计算。结果，每次重复都要进行 16 个连接操作，这些操作针对不断增长的 <code class="ce">sHTML</code> 进行。在新版本中，我们提示编译器更改操作顺序。现在，它将按从左到右、从括号内到括号外的顺序计算表达式。此技术使得每次重复包括 15 个连接操作，这些操作针对的是不会增长的较小字符串，只有一个是针对不断增长的大的 <code class="ce">sHTML</code>。图 1 显示了这种优化方法与标准连接方法在内存使用模式方面的比较。</p><p class="fig" align="center"><img border="0" alt="" src="http://www.dazix.cn/upload/2010/3/201003261225513752.gif" /></p><p class="label"><b>图 1：标准连接与加括号连接在内存使用模式方面的比较</b></p><p>在特定情况下，使用括号可以对性能和可缩放性产生十分显著的影响，后文将对此进行进一步的说明。</p><h2 class="dtH1"><a name="aspstrcatn_stringbuilder"></a>StringBuilder</h2><p>我们已经找到了解决字符串连接问题的快捷方法，在多数情况下，此方法可以达到性能和投入的最佳平衡。但是，如果要进一步提高构建大型字符串的性能，需要采用第二种方法，即减少字符串分配操作的数目。为此，需要使用 <b>StringBuilder</b>。StringBuilder 是一个类，用于维护可配置的字符串缓冲区，管理插入到此缓冲区的新文本片断，并仅在文本长度超出字符串缓冲区长度时对字符串进行重新分配。Microsoft .NET 框架免费提供了这样一个类 (<b>System.Text.StringBuilder</b>)，并建议在该环境下进行的所有字符串连接操作中使用它。在 ASP 和传统的 Visual Basic 环境中，我们无法访问此类，因此需要自行创建。下面是使用 Visual Basic 6.0 创建的 <b>StringBuilder</b> 类示例（为简洁起见，省略了错误处理代码）。</p><pre class="code">Option Explicit'默认的缓冲区初始大小和增长系数Private Const DEF_INITIALSIZE As Long = 1000Private Const DEF_GROWTH As Long = 1000' 缓冲区大小和增长Private m_nInitialSize As LongPrivate m_nGrowth As Long' 缓冲区和缓冲区计数器Private m_sText As StringPrivate m_nSize As LongPrivate m_nPos As LongPrivate Sub Class_Initialize()&nbsp;&nbsp;&nbsp;&nbsp;' 设置大小和增长的默认值    m_nInitialSize = DEF_INITIALSIZE    m_nGrowth = DEF_GROWTH    ' 初始化缓冲区    InitBufferEnd Sub' 设置初始大小和增长数量Public Sub Init(ByVal InitialSize As Long, ByVal Growth As Long)    If InitialSize &gt; 0 Then m_nInitialSize = InitialSize    If Growth &gt; 0 Then m_nGrowth = GrowthEnd Sub' 初始化缓冲区Private Sub InitBuffer()    m_nSize = -1    m_nPos = 1End Sub' 增大缓冲区Private Sub Grow(Optional MinimimGrowth As Long)    ' 初始化缓冲区（如有必要）    If m_nSize = -1 Then         m_nSize = m_nInitialSize             m_sText = Space$(m_nInitialSize)       Else&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' 只是增长&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dim nGrowth As Long&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nGrowth = IIf(m_nGrowth &gt; MinimimGrowth, m_nGrowth, MinimimGrowth)  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_nSize = m_nSize + nGrowth        m_sText = m_sText &amp; Space$(nGrowth)    End IfEnd Sub' 将缓冲区大小调整到当前使用的大小Private Sub Shrink()    If m_nSize &gt; m_nPos Then        m_nSize = m_nPos - 1        m_sText = RTrim$(m_sText)    End IfEnd Sub' 添加单个文本字符串Private Sub AppendInternal(ByVal Text As String)    If (m_nPos + Len(Text)) &gt; m_nSize Then&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Grow Len(Text)&nbsp;&nbsp;&nbsp;&nbsp;Mid$(m_sText, m_nPos, Len(Text)) = Text&nbsp;&nbsp;&nbsp;&nbsp;m_nPos = m_nPos + Len(Text)End Sub' 添加一些文本字符串Public Sub Append(ParamArray Text())    Dim nArg As Long    For nArg = 0 To UBound(Text)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AppendInternal CStr(Text(nArg))    Next nArgEnd Sub' 返回当前字符串数据并调整缓冲区大小Public Function ToString() As String    If m_nPos &gt; 0 Then&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Shrink&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ToString = m_sText    Else&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ToString = &quot;&quot;    End IfEnd Function' 清除缓冲区并重新初始化Public Sub Clear()    InitBufferEnd Sub</pre><p>此类中使用的基本原则是，在类级别将变量 (<code class="ce">m_sText</code>) 用作字符串缓冲区，并使用 <b>Space$</b> 函数以空格字符填充此缓冲区以将其设置为特定的大小。如果要将更多文本与现有文本连接在一起，则在检查缓冲区的大小足以存放新文本后，使用 <b>Mid$</b> 函数在正确位置插入文本。<b>ToString</b> 函数将返回当前存储在缓冲区中的文本，并将缓冲区的大小调整为能够容纳此文本的正确长度。使用 <b>StringBuilder</b> 的 ASP 代码如下所示：</p><pre class="code">Function WriteHTML( Data )&nbsp;&nbsp;&nbsp;&nbsp;Dim oSBDim nRepSet oSB = Server.CreateObject( &quot;StringBuilderVB.StringBuilder&quot; )    ' 用大小和增长系数初始化缓冲区    oSB.Init 15000, 7500    For nRep = 0 to 99        oSB.Append &quot;&lt;TR&gt;&lt;TD&gt;&quot;, (nRep + 1), &quot;&lt;/TD&gt;&lt;TD&gt;&quot;,        Data( 0, nRep ), &quot;&lt;/TD&gt;&lt;TD&gt;&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Data( 1, nRep ), &quot;&lt;/TD&gt;&lt;TD&gt;&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Data( 2, nRep ), &quot;&lt;/TD&gt;&lt;TD&gt;&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Data( 3, nRep ), &quot;&lt;/TD&gt;&lt;TD&gt;&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Data( 4, nRep ), &quot;&lt;/TD&gt;&lt;TD&gt;&quot;,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Data( 5, nRep ), &quot;&lt;/TD&gt;&lt;/TR&gt;&quot;    Next    WriteHTML = oSB.ToString()    Set oSB = NothingEnd Function</pre><p>使用 <b>StringBuilder</b> 需要一定的开销，因为每次使用此类时都必须创建它的实例，并且在创建第一个类实例时必须加载包含此类的 DLL。对 <b>StringBuilder</b> 实例进行额外方法调用时也需要开销。使用加括号的&ldquo;<code class="ce">&amp;</code>&rdquo;方法时，<b>StringBuilder</b> 如何执行取决于多个因素，包括连接的数目、要构建的字符串的大小以及选择的 StringBuilder 字符串缓冲区的初始化参数的性能。请注意，在多数情况下，将缓冲区中所需的空间量估计得略高一些要远远好于让其不断增长。</p><h2 class="dtH1"><a name="aspstrcatn_responsewrite"></a>内置方法</h2><p>ASP 包含一种非常快捷的创建 HTML 代码的方法，只需多次调用 <b>Response.Write</b>。<b>Write</b> 函数使用隐式优化的字符串缓冲区，此缓冲区能够提供非常优秀的性能特性。修改后的 <b>WriteHTML</b> 代码如下所示：</p><pre class="code">Function WriteHTML( Data )Dim nRepFor nRep = 0 to 99    Response.Write &quot;&lt;TR&gt;&lt;TD&gt;&quot;    Response.Write (nRep + 1)    Response.Write &quot;&lt;/TD&gt;&lt;TD&gt;&quot;    Response.Write Data( 0, nRep )    Response.Write &quot;&lt;/TD&gt;&lt;TD&gt;&quot;    Response.Write Data( 1, nRep )    Response.Write &quot;&lt;/TD&gt;&lt;TD&gt;&quot;    Response.Write Data( 2, nRep )    Response.Write &quot;&lt;/TD&gt;&lt;TD&gt;&quot;    Response.Write Data( 3, nRep )    Response.Write &quot;&lt;/TD&gt;&lt;TD&gt;&quot;    Response.Write Data( 4, nRep )    Response.Write &quot;&lt;/TD&gt;&lt;TD&gt;&quot;    Response.Write Data( 5, nRep )    Response.Write &quot;&lt;/TD&gt;&lt;/TR&gt;NextEnd Function</pre><p>虽然这段代码很可能为我们提供最佳的性能和可缩放性，但在某种程度上已经破坏了封装，因为现在会将函数内部的代码直接写入 <b>Response</b> 流，所以调用代码丧失了一定程度的控制权。另外，移动此代码（例如，移入 COM 组件）将变得更加困难，因为此函数与 <b>Response</b> 流存在依赖关系。</p><h2 class="dtH1"><a name="aspstrcatn_testing"></a>测试</h2><p>上面提到的四种方法分别通过一个简单的 ASP 页面（包含一个由虚拟字符串数组提供数据的单个表格）进行了测试。我们使用 Application Center Test&reg; (ACT) 从单个客户端（Windows&reg; XP Professional，PIII-850MHz，512MB RAM）针对 100Mb/sec 网络中的单个服务器（Windows 2000 Advanced Server，双 PIII-1000MHz，256MB RAM）执行了测试。ACT 配置为使用 5 个线程，以模拟 5 个用户连接至网站时的负载。每个测试都包括 20 秒预热时间和随后的 100 秒负载时间，在负载期间创建了尽可能多的请求。</p><p>通过更改主表格循环中的重复次数，针对不同数目的连接操作重复运行测试，如 <b>WriteHTML</b> 函数中的代码片断所示。运行的每个测试都使用上文提到的四种不同的方法执行。</p><h2 class="dtH1"><a name="aspstrcatn_results"></a>结果</h2><p>下面的一系列图表显示了各种方法对整个应用程序吞吐量的影响，以及 ASP 页面的响应时间。通过这些图表，我们可以了解应用程序支持的请求数目，以及用户等待页面下载至浏览器所需的时间。</p><p class="label"><b>表 1：使用的连接方法缩写的说明</b></p><p><table class="data">    <tbody>        <tr valign="top">            <th class="data" width="35%" align="left">方法缩写</th>            <th class="data" width="65%" align="left">说明</th>        </tr>        <tr valign="top">            <td class="data" width="35%">RESP</td>            <td class="data" width="65%">内置 <b>Response.Write</b> 方法</td>        </tr>        <tr valign="top">            <td class="data" width="35%">CAT</td>            <td class="data" width="65%">标准连接（&ldquo;<code class="ce">&amp;</code>&rdquo;）方法</td>        </tr>        <tr valign="top">            <td class="data" width="35%">PCAT</td>            <td class="data" width="65%">加括号的连接（&ldquo;<code class="ce">&amp;</code>&rdquo;）方法</td>        </tr>        <tr valign="top">            <td class="data" width="35%">BLDR</td>            <td class="data" width="65%"><b>StringBuilder</b> 方法</td>        </tr>    </tbody></table></p><p>在模拟典型 ASP 应用程序工作负荷方面，此测试与实际情况相差甚远，从表 2 中可以明显看到，即使重复 420 次，此页面仍不是特别大。现在很多复杂的 ASP 页面在这些数字上都是比较高的，设置有可能超出此测试范围的限制。</p><p class="label"><b>表 2：测试示例的页面大小和连接数目</b></p><p><table class="data" align="center">    <tbody>        <tr valign="top">            <td class="data" width="28%"><b>重复次数</b></td>            <td class="data" width="40%"><b>连接数目</b></td>            <td class="data" width="32%"><b>页面大小（以字节为单位）</b></td>        </tr>        <tr valign="top">            <td class="data" width="28%">15</td>            <td class="data" width="40%">240</td>            <td class="data" width="32%">2,667</td>        </tr>        <tr valign="top">            <td class="data" width="28%">30</td>            <td class="data" width="40%">480</td>            <td class="data" width="32%">4,917</td>        </tr>        <tr valign="top">            <td class="data" width="28%">45</td>            <td class="data" width="40%">720</td>            <td class="data" width="32%">7,167</td>        </tr>        <tr valign="top">            <td class="data" width="28%">60</td>            <td class="data" width="40%">960</td>            <td class="data" width="32%">9,417</td>        </tr>        <tr valign="top">            <td class="data" width="28%">75</td>            <td class="data" width="40%">1,200</td>            <td class="data" width="32%">11,667</td>        </tr>        <tr valign="top">            <td class="data" width="28%">120</td>            <td class="data" width="40%">1,920</td>            <td class="data" width="32%">18,539</td>        </tr>        <tr valign="top">            <td class="data" width="28%">180</td>            <td class="data" width="40%">2,880</td>            <td class="data" width="32%">27,899</td>        </tr>        <tr valign="top">            <td class="data" width="28%">240</td>            <td class="data" width="40%">3,840</td>            <td class="data" width="32%">37,259</td>        </tr>        <tr valign="top">            <td class="data" width="28%">300</td>            <td class="data" width="40%">4,800</td>            <td class="data" width="32%">46,619</td>        </tr>        <tr valign="top">            <td class="data" width="28%">360</td>            <td class="data" width="40%">5,760</td>            <td class="data" width="32%">55,979</td>        </tr>        <tr valign="top">            <td class="data" width="28%">420</td>            <td class="data" width="40%">6,720</td>            <td class="data" width="32%">62,219</td>        </tr>    </tbody></table></p><p class="fig" align="center"><img border="0" alt="" src="http://www.dazix.cn/upload/2010/3/201003261225521124.gif" /></p><p class="label"><b>图 2：吞吐量结果图</b></p><p>从图 2 的图表中可以看到，正如我们所预期的，多重 <b>Response.Write</b> 方法 (RESP) 在测试的整个重复测试范围中为我们提供了最佳的吞吐量。但令人惊讶的是，标准字符串连接方法 (CAT) 的下降如此巨大，而加括号的方法 (PCAT) 在重复执行 300 多次时性能依旧要好很多。在大约重复 220 次之处，字符串缓存带来的性能提高超过了 <b>StringBuilder</b> 方法 (BLDR) 固有的开销，在这一点以上，在此 ASP 页面中使用 StringBuilder 所需的额外开销是值得的。</p><p class="fig" align="center"><img border="0" alt="" src="http://www.dazix.cn/upload/2010/3/201003261225522155.gif" /></p><p class="label"><b>图 3：响应时间结果图</b></p><p class="fig" align="center"><img border="0" alt="" src="http://www.dazix.cn/upload/2010/3/201003261225525731.gif" /></p><p class="label"><b>图 4：省略 CAT 的响应时间结果图</b></p><p>图 3 和图 4 中的图表显示了按&ldquo;到第一字节的时间&rdquo;测量的响应时间（以毫秒为单位）。因为标准字符串连接方法 (CAT) 的响应时间增加过快，所以又提供了未包括此方法的图表（图 4），以便分析其他方法之间的差异。有一点值得注意，多重 <b>Response.Write</b> 方法 (RESP) 和 <b>StringBuilder</b> 方法 (BLDR) 随重复次数的增加呈现一种近似线性的增长，而标准连接方法 (CAT) 和加括号的方法 (PCAT) 则在超过一定的阈值之后开始迅速增加。</p><h2 class="dtH1"><a name="aspstrcatn_conclusion"></a>小结</h2><p>本文着重讲述了如何在 ASP 环境中应用不同的字符串构建技术，这些内容同样适用于所有使用 Visual Basic 代码创建大型字符串的方案，例如手动创建 XML 文档。以下原则可以帮助您确定哪种方法最适合您的需要。</p><ul type="disc">    <li>首先尝试加括号的&ldquo;<code class="ce">&amp;</code>&rdquo;方法，尤其是在处理现有代码时。这种方法对代码结构的影响微乎其微，但您会发现应用程序的性能将显著增强，甚至会超出预定目标。</li>    <li>在不破坏所需的封装级别的情况下使用 <b>Response.Write</b>。使用此方法，可以避免不必要的内存内字符串处理，从而提供最佳的性能。</li>    <li>使用 <b>StringBuilder</b> 构建真正大型或连接数目较多的字符串。</li></ul><p>尽管您可能未看到本文所示的这种性能增长，但我已在真实的 ASP Web 应用程序中使用了这些技巧，只需要很少的额外投入就可以在性能和可缩放性方面获得很大的提高。</p>]]></description><category>其他</category><comments>http://www.dazix.cn/post/67.html#comment</comments><wfw:comment>http://www.dazix.cn/</wfw:comment><wfw:commentRss>http://www.dazix.cn/feed.asp?cmt=67</wfw:commentRss><trackback:ping>http://www.dazix.cn/cmd.asp?act=tb&amp;id=67&amp;key=dbdac8ed</trackback:ping></item><item><title>ASP Request.ServerVariables获取环境变量集合</title><author>30538357@qq.com (dazix)</author><link>http://www.dazix.cn/post/64.html</link><pubDate>Thu, 18 Mar 2010 13:24:41 +0800</pubDate><guid>http://www.dazix.cn/post/64.html</guid><description><![CDATA[<p>Request是asp的六大内置对象之一<br />ServerVariables是环境变量的集合。允许读取HTTP头。可以通过使用HTTP_前缀来读取任何头信息。比如HTTP_USER_AGENT接受客户代理HTTP头(浏览器类型)。此外还可以使用以下变量获得相应环境信息。<br />ALL_HTTP<br />客户端发送的所有HTTP标头，他的结果都有前缀HTTP_。<br /><br />ALL_RAW<br />客户端发送的所有HTTP标头,其结果和客户端发送时一样，没有前缀HTTP_ <br /><br />APPL_MD_PATH<br />应用程序的元数据库路径。<br /><br />APPL_PHYSICAL_PATH<br />与应用程序元数据库路径相应的物理路径。<br /><br />AUTH_PASSWORD<br />当使用基本验证模式时，客户在密码对话框中输入的密码。<br /><br />AUTH_TYPE<br />这是用户访问受保护的脚本时，服务器用于检验用户的验证方法。<br /><br />AUTH_USER<br />代验证的用户名。<br /><br />CERT_COOKIE<br />唯一的客户证书ID号。<br /><br />CERT_FLAG<br />客户证书标志，如有客户端证书，则bit0为0。如果客户端证书验证无效，bit1被设置为1。<br /><br />CERT_ISSUER<br />用户证书中的发行者字段。<br /><br />CERT_KEYSIZE<br />安全套接字层连接关键字的位数，如128。<br /><br />CERT_SECRETKEYSIZE<br />服务器验证私人关键字的位数。如1024。<br /><br />CERT_SERIALNUMBER<br />客户证书的序列号字段。<br /><br />CERT_SERVER_ISSUER<br />服务器证书的发行者字段<br /><br />CERT_SERVER_SUBJECT<br />服务器证书的主题字段。<br /><br />CERT_SUBJECT<br />客户端证书的主题字段。<br /><br />CONTENT_LENGTH<br />客户端发出内容的长度。<br /><br />CONTENT_TYPE<br />客户发送的form内容或HTTP PUT的数据类型。<br /><br />GATEWAY_INTERFACE<br />服务器使用的网关界面。<br /><br />HTTPS<br />如果请求穿过安全通道（SSL），则返回ON。如果请求来自非安全通道，则返回OFF。<br /><br />HTTPS_KEYSIZE<br />安全套接字层连接关键字的位数，如128。<br /><br />HTTPS_SECRETKEYSIZE<br />服务器验证私人关键字的位数。如1024。<br /><br />HTTPS_SERVER_ISSUER<br />服务器证书的发行者字段。<br /><br />HTTPS_SERVER_SUBJECT<br />服务器证书的主题字段。<br /><br />INSTANCE_ID<br />IIS实例的ID号。<br /><br />INSTANCE_META_PATH<br />响应请求的IIS实例的元数据库路径。<br /><br />LOCAL_ADDR<br />返回接受请求的服务器地址。<br /><br />LOGON_USER<br />用户登录Windows NT的帐号<br /><br />PATH_INFO<br />客户端提供的路径信息。<br /><br />PATH_TRANSLATED<br />通过由虚拟至物理的映射后得到的路径。<br /><br />QUERY_STRING<br />查询字符串内容。<br /><br />REMOTE_ADDR<br />发出请求的远程主机的IP地址。<br /><br />REMOTE_HOST<br />发出请求的远程主机名称。<br /><br />REQUEST_METHOD<br />提出请求的方法。比如GET、HEAD、POST等等。<br /><br />SCRIPT_NAME<br />执行脚本的名称。<br /><br />SERVER_NAME<br />服务器的主机名、DNS地址或IP地址。<br /><br />SERVER_PORT<br />接受请求的服务器端口号。<br /><br />SERVER_PORT_SECURE<br />如果接受请求的服务器端口为安全端口时，则为1，否则为0。<br /><br />SERVER_PROTOCOL<br />服务器使用的协议的名称和版本。<br /><br />SERVER_SOFTWARE<br />应答请求并运行网关的服务器软件的名称和版本。<br /><br />URL<br />提供URL的基本部分。</p>]]></description><category>其他</category><comments>http://www.dazix.cn/post/64.html#comment</comments><wfw:comment>http://www.dazix.cn/</wfw:comment><wfw:commentRss>http://www.dazix.cn/feed.asp?cmt=64</wfw:commentRss><trackback:ping>http://www.dazix.cn/cmd.asp?act=tb&amp;id=64&amp;key=bcfc534e</trackback:ping></item><item><title>asp获取网址url相关内容的操作 URL Request</title><author>30538357@qq.com (dazix)</author><link>http://www.dazix.cn/post/63.html</link><pubDate>Thu, 18 Mar 2010 12:21:55 +0800</pubDate><guid>http://www.dazix.cn/post/63.html</guid><description><![CDATA[<p><strong>一、获取地址中的文件名，不包含扩展名</strong><br />&lt;%<br />dim Url,FileName,File<br />Url=split(request.servervariables(&quot;script_name&quot;),&quot;/&quot;)<br />FileName=Url(ubound(Url))<br />File=Left(FileName,InstrRev(FileName,&quot;.&quot;)-1)<br />Response.Write &quot;文件名：&quot;&amp; File<br />%&gt;<br /><strong>二、获取地址参数</strong><br />&lt;%<br />dim url<br />url=url&amp;&quot;http://&quot;&amp;request.ServerVariables(&quot;Server_NAME&quot;)&amp;request.ServerVariables(&quot;SCRIPT_NAME&quot;)<br />response.Write url<br />%&gt;<br /><strong>三、获取全部地址参数，包括&amp;及后面的参数</strong><br />&lt;%<br />dim url<br />url=url&amp;&quot;http://&quot;&amp;request.ServerVariables(&quot;Server_NAME&quot;)&amp;request.ServerVariables(&quot;SCRIPT_NAME&quot;)<br />if(len(trim(request.ServerVariables(&quot;QUERY_STRING&quot;)))&gt;0) then<br />url=url &amp; &quot;&amp;&quot; &amp; request.ServerVariables(&quot;QUERY_STRING&quot;)<br />end if<br />response.Write url<br />%&gt;<br />&lt;%<br />If Request.QueryString.Count&gt;0 Then<br />For each querystring in Request.QueryString<br />&nbsp;&nbsp; query = query+querystring&amp;&quot;=&quot;&amp;Request.QueryString(querystring)&amp;&quot;&amp;&quot;<br />Next<br />&nbsp;&nbsp; query = mid(query,1,len(query)-1)<br />&nbsp;&nbsp; Response.Write &quot;http://&quot;&amp;Request.ServerVariables(&quot;SERVER_NAME&quot;)&amp;Request.ServerVariables(&quot;PATH_INFO&quot;)&amp;&quot;&amp;&quot;&amp;query<br />Else<br />&nbsp;&nbsp; Response.Write &quot;http://&quot;&amp;Request.ServerVariables(&quot;SERVER_NAME&quot;)&amp;Request.ServerVariables(&quot;PATH_INFO&quot;)<br />End If<br />%&gt;<br /><strong>四、域名完整地址:包括&quot;http://&nbsp;&quot;和域名尾部的&quot;/&quot;</strong><br />&lt;%<br />Response.write(&quot;http://&quot; &amp; Request.ServerVariables(&quot;HTTP_HOST&quot;) &amp; Mid(Request.ServerVariables(&quot;URL&quot;),1,InStrRev(Request.ServerVariables(&quot;URL&quot;),&quot;/&quot;)))<br />%&gt;<br /><strong>五、获取域名地址 形如：</strong><a title="IT备忘录" href="http://www.dazix.cn"><strong>www.dazix.cn</strong></a><strong>&nbsp;域名</strong><br />&lt;%=Request.ServerVariables(&quot;HTTP_HOST&quot;) %&gt;<br /><strong>六、获取页面地址</strong><br />&lt;%<br />Response.write Request.ServerVariables(&quot;URL&quot;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lsquo;形如：/index.asp<br />Response.write request.servervariables(&quot;script_name&quot;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&rsquo;形如：/index.asp<br />%&gt;<br /><strong>七、 获取客户端IP地址</strong><br />&lt;%=Request.ServerVariables(&quot;REMOTE_ADDR&quot;) %&gt;<br /><strong>八、获取服务器IP地址</strong><br />&lt;%=Request.ServerVariables(&quot;LOCAL_ADDR&quot;) %&gt;<br /><strong>九、可获取父页面地址,然后进行处理</strong><br />&lt;a href=&quot;&lt;%=request.serverVariables(&quot;Http_REFERER&quot;) %&gt;&quot;&gt;返回前页&lt;/a&gt;<br /><strong>十、asp 获取网页地址及参数<br /></strong>&lt;%=Request.ServerVariables(&quot;HTTP_HOST&quot;) %&gt;<!-- google_ad_section_end --><br /><strong>十一、获取域名</strong><br />&lt;%=Request.ServerVariables(&quot;PATH_INFO&quot;) %&gt;<br /><strong>十二、获取参数集</strong><br />&lt;%=Request.ServerVariables(&quot;QUERY_STRING&quot;) %&gt;</p><!-- google_ad_section_end -->]]></description><category>其他</category><comments>http://www.dazix.cn/post/63.html#comment</comments><wfw:comment>http://www.dazix.cn/</wfw:comment><wfw:commentRss>http://www.dazix.cn/feed.asp?cmt=63</wfw:commentRss><trackback:ping>http://www.dazix.cn/cmd.asp?act=tb&amp;id=63&amp;key=3f299152</trackback:ping></item><item><title>PHP 5.3 For Windows Binaries 版本说明</title><author>30538357@qq.com (dazix)</author><link>http://www.dazix.cn/post/38.html</link><pubDate>Fri, 23 Oct 2009 09:59:24 +0800</pubDate><guid>http://www.dazix.cn/post/38.html</guid><description><![CDATA[<p>PHP For Windows Binaries<br />现在推出5.3.0版本了，不过下载的时候有几个不同版本选择。那就是VC6 X86和VC9 X86。</p><p>VC6是什么？<br />VC6就是legacy Visual Studio 6 compiler，就是使用这个编译器编译的。<br />VC9是什么？<br />VC9就是the Visual Studio 2008 compiler，就是用微软的VS编辑器编译的。</p><p>在windows下使用Apache+PHP的，请选择VC6版本；<br />在windows下使用IIS+PHP的，请选择VC9版本；</p><p>Thread Safe是线程安全，执行时会进行线程（Thread）安全检查，以防止有新要求就启动新线程的CGI执行方式而耗尽系统资源。Non Thread Safe是非线程安全，在执行时不进行线程（Thread）安全检查。</p><p>那Non Thread Safe是什么？<br />Non Thread Safe就是非线程安全；<br />Thread Safe 是什么?<br />Thread Safe 是线程安全；</p><p>官方并不建议你将Non Thread Safe 应用于生产环境，所以我们选择Thread Safe 版本的PHP来使用。<br />&nbsp;<br />再来看PHP的两种执行方式：ISAPI和FastCGI。</p><p>ISAPI执行方式是以DLL动态库的形式使用，可以在被用户请求后执行，在处理完一个用户请求后不会马上消失，所以需要进行线程安全检查，这样来提高程序的执行效率，所以如果是以ISAPI来执行PHP，建议选择Thread Safe版本；<br />而FastCGI执行方式是以单一线程来执行操作，所以不需要进行线程的安全检查，除去线程安全检查的防护反而可以提高执行效率，所以，如果是以FastCGI来执行PHP，建议选择Non Thread Safe版本</p>]]></description><category>其他</category><comments>http://www.dazix.cn/post/38.html#comment</comments><wfw:comment>http://www.dazix.cn/</wfw:comment><wfw:commentRss>http://www.dazix.cn/feed.asp?cmt=38</wfw:commentRss><trackback:ping>http://www.dazix.cn/cmd.asp?act=tb&amp;id=38&amp;key=d7864f3b</trackback:ping></item><item><title>ASP 错误收集</title><author>30538357@qq.com (dazix)</author><link>http://www.dazix.cn/post/35.html</link><pubDate>Tue, 29 Sep 2009 14:30:10 +0800</pubDate><guid>http://www.dazix.cn/post/35.html</guid><description><![CDATA[<p class="style1">对初学者们有帮助,或许高手也会忘记<br />MicrosoftVBScript语法错误(0x800A03E9)--&gt;内存不足<br />MicrosoftVBScript语法错误(0x800A03EA)--&gt;语法错误<br />MicrosoftVBScript语法错误(0x800A03EB)--&gt;缺少 ':'<br />MicrosoftVBScript语法错误(0x800A03ED)--&gt;缺少 '('<br />MicrosoftVBScript语法错误(0x800A03EE)--&gt;缺少 ')'<br />MicrosoftVBScript语法错误(0x800A03EF)--&gt;缺少 ']'<br />MicrosoftVBScript语法错误(0x800A03F2)--&gt;缺少标识符<br />MicrosoftVBScript语法错误(0x800A03F3)--&gt;缺少 '='<br />MicrosoftVBScript语法错误(0x800A03F4)--&gt;缺少 'If'<br />MicrosoftVBScript语法错误(0x800A03F5)--&gt;缺少 'To'<br />MicrosoftVBScript语法错误(0x800A03F6)--&gt;缺少 'End'<br />MicrosoftVBScript语法错误(0x800A03F7)--&gt;缺少 'Function'<br />MicrosoftVBScript语法错误(0x800A03F8)--&gt;缺少 'Sub'<br />MicrosoftVBScript语法错误(0x800A03F9)--&gt;缺少 'Then'<br />MicrosoftVBScript语法错误(0x800A03FA)--&gt;缺少 'Wend'<br />MicrosoftVBScript语法错误(0x800A03FB)--&gt;缺少 'Loop'<br />MicrosoftVBScript语法错误(0x800A03FC)--&gt;缺少 'Next'<br />MicrosoftVBScript语法错误(0x800A03FD)--&gt;缺少 'Case'<br />MicrosoftVBScript语法错误(0x800A03FE)--&gt;缺少 'Select'<br />MicrosoftVBScript语法错误(0x800A03FF)--&gt;缺少表达式<br />MicrosoftVBScript语法错误(0x800A0400)--&gt;缺少语句<br />MicrosoftVBScript语法错误(0x800A0401)--&gt;语句未结束 <br />MicrosoftVBScript语法错误(0x800A0402)--&gt;缺少整型常数 <br />MicrosoftVBScript语法错误(0x800A0403)--&gt;缺少 'While' 或 'Until'<br />MicrosoftVBScript语法错误(0x800A0404)--&gt;缺少 'While', 'Until' 或语句未结束<br />MicrosoftVBScript语法错误(0x800A0405)--&gt;缺少 'With'<br />MicrosoftVBScript语法错误(0x800A0406)--&gt;标识符过长<br />MicrosoftVBScript语法错误(0x800A0407)--&gt;无效数字<br />MicrosoftVBScript语法错误(0x800A0408)--&gt;无效字符<br />MicrosoftVBScript语法错误(0x800A0409)--&gt;未结束的字符串常量<br />MicrosoftVBScript语法错误(0x800A040A)--&gt;注释未结束<br />MicrosoftVBScript语法错误(0x800A040D)--&gt;无效使用 'Me' 关键字<br />MicrosoftVBScript语法错误(0x800A040E)--&gt;'loop' 语句缺少 'do'<br />MicrosoftVBScript语法错误(0x800A040F)--&gt;无效的 'exit' 语句<br />MicrosoftVBScript语法错误(0x800A0410)--&gt;循环控制变量 'for' 无效<br />MicrosoftVBScript语法错误(0x800A0411)--&gt;名称重定义<br />MicrosoftVBScript语法错误(0x800A0412)--&gt;必须是行中的第一个语句<br />MicrosoftVBScript语法错误(0x800A0413)--&gt;不能为 non-ByVal 参数赋值<br />MicrosoftVBScript语法错误(0x800A0414)--&gt;调用子程序时不能使用括号<br />MicrosoftVBScript语法错误(0x800A0415)--&gt;缺少文字常数<br />MicrosoftVBScript语法错误(0x800A0416)--&gt;缺少 'In'<br />MicrosoftVBScript语法错误(0x800A0417)--&gt;缺少 'Class'<br />MicrosoftVBScript语法错误(0x800A0418)--&gt;必须在一个类的内部定义<br />MicrosoftVBScript语法错误(0x800A0419)--&gt;在属性声明中缺少 Let , Set 或 Get<br />MicrosoftVBScript语法错误(0x800A041A)--&gt;缺少 'Property'<br />MicrosoftVBScript语法错误(0x800A041B)--&gt;在所有属性的规范中，变量的数目必须一致<br />MicrosoftVBScript语法错误(0x800A041C)--&gt;在一个类中不允许有多个缺省的属性/方法<br />MicrosoftVBScript语法错误(0x800A041D)--&gt;类的初始化或终止程序没有参数<br />MicrosoftVBScript语法错误(0x800A041E)--&gt;属性的 set 或 let 必须至少有一个参数<br />MicrosoftVBScript语法错误(0x800A041F)--&gt;错误的 'Next'<br />MicrosoftVBScript语法错误(0x800A0420)--&gt;'Default' 只能在 'Property' , 'Function' 或 'Sub' 中指定<br />MicrosoftVBScript语法错误(0x800A0421)--&gt;指定 'Default' 时必须同时指定 'Public' &quot;)<br />MicrosoftVBScript语法错误(0x800A0422)--&gt;只能在 Property Get 中指定 'Default'<br />MicrosoftVBScript 运行时错误(0x800A0005)--&gt;无效的过程调用或参数<br />MicrosoftVBScript 运行时错误(0x800A0006)--&gt;溢出<br />MicrosoftVBScript 运行时错误(0x800A0007)--&gt;内存不足<br />MicrosoftVBScript 运行时错误(0x800A0009)--&gt;下标越界<br />MicrosoftVBScript 运行时错误(0x800A000A)--&gt;该数组为定长的或临时被锁定<br />MicrosoftVBScript 运行时错误(0x800A000B)--&gt;被零除<br />MicrosoftVBScript 运行时错误(0x800A000D)--&gt;类型不匹配<br />MicrosoftVBScript 运行时错误(0x800A000E)--&gt;字符串空间不够<br />MicrosoftVBScript 运行时错误(0x800A0011)--&gt;不能执行所需的操作<br />MicrosoftVBScript 运行时错误(0x800A001C)--&gt;堆栈溢出<br />MicrosoftVBScript 运行时错误(0x800A0023)--&gt;未定义过程或函数<br />MicrosoftVBScript 运行时错误(0x800A0030)--&gt;加载 DLL 时出错<br />MicrosoftVBScript 运行时错误(0x800A0033)--&gt;内部错误<br />MicrosoftVBScript 运行时错误(0x800A0034)--&gt;错误的文件名或号码<br />MicrosoftVBScript 运行时错误(0x800A0035)--&gt;文件未找到<br />MicrosoftVBScript 运行时错误(0x800A0036)--&gt;错误的文件模式<br />MicrosoftVBScript 运行时错误(0x800A0037)--&gt;文件已经打开 <br />MicrosoftVBScript 运行时错误(0x800A0039)--&gt;设备 I/O 错误<br />MicrosoftVBScript 运行时错误(0x800A003A)--&gt;文件已存在<br />MicrosoftVBScript 运行时错误(0x800A003D)--&gt;磁盘已满<br />MicrosoftVBScript 运行时错误(0x800A003E)--&gt;输入超出了文件尾<br />MicrosoftVBScript 运行时错误(0x800A0043)--&gt;文件过多<br />MicrosoftVBScript 运行时错误(0x800A0044)--&gt;设备不可用<br />MicrosoftVBScript 运行时错误(0x800A0046)--&gt;没有权限<br />MicrosoftVBScript 运行时错误(0x800A0047)--&gt;磁盘没有准备好<br />MicrosoftVBScript 运行时错误(0x800A004A)--&gt;重命名时不能带有其他驱动器符号<br />MicrosoftVBScript 运行时错误(0x800A004B)--&gt;路径/文件访问错误<br />MicrosoftVBScript 运行时错误(0x800A004C)--&gt;路径未找到<br />MicrosoftVBScript 运行时错误(0x800A005B)--&gt;对象变量未设置<br />MicrosoftVBScript 运行时错误(0x800A005C)--&gt;For 循环未初始化<br />MicrosoftVBScript 运行时错误(0x800A005E)--&gt;无效使用 Null<br />MicrosoftVBScript 运行时错误(0x800A0142)--&gt;不能创建所需的临时文件<br />MicrosoftVBScript 运行时错误(0x800A01A8)--&gt;缺少对象<br />MicrosoftVBScript 运行时错误(0x800A01AD)--&gt;ActiveX 部件不能创建对象<br />MicrosoftVBScript 运行时错误(0x800A01AE)--&gt;类不能支持 Automation 操作<br />MicrosoftVBScript 运行时错误(0x800A01B0)--&gt;Automation 操作中文件名或类名未找到<br />MicrosoftVBScript 运行时错误(0x800A01B6)--&gt;对象不支持此属性或方法<br />MicrosoftVBScript 运行时错误(0x800A01B8)--&gt;Automation 操作错误<br />MicrosoftVBScript 运行时错误(0x800A01BD)--&gt;对象不支持此操作<br />MicrosoftVBScript 运行时错误(0x800A01BE)--&gt;对象不支持已命名参数<br />MicrosoftVBScript 运行时错误(0x800A01BF)--&gt;对象不支持当前区域设置<br />MicrosoftVBScript 运行时错误(0x800A01C0)--&gt;未找到已命名参数<br />MicrosoftVBScript 运行时错误(0x800A01C1)--&gt;参数是必选项<br />MicrosoftVBScript 运行时错误(0x800A01C2)--&gt;错误的参数个数或无效的参数属性值<br />MicrosoftVBScript 运行时错误(0x800A01C3)--&gt;对象不是一个集合<br />MicrosoftVBScript 运行时错误(0x800A01C5)--&gt;未找到指定的 DLL 函数<br />MicrosoftVBScript 运行时错误(0x800A01C7)--&gt;代码资源锁定错误<br />MicrosoftVBScript 运行时错误(0x800A01CA)--&gt;变量使用了一个 VBScript 中不支持的 Automation 类型<br />MicrosoftVBScript 运行时错误(0x800A01CE)--&gt;远程服务器不存在或不可用<br />MicrosoftVBScript 运行时错误(0x800A01E1)--&gt;无效图片<br />MicrosoftVBScript 运行时错误(0x800A01F4)--&gt;变量未定义<br />MicrosoftVBScript 运行时错误(0x800A01F5)--&gt;非法赋值<br />MicrosoftVBScript 运行时错误(0x800A01F6)--&gt;对象不能安全地使用 Script 编程<br />MicrosoftVBScript 运行时错误(0x800A01F7)--&gt;对象不能安全初始化<br />MicrosoftVBScript 运行时错误(0x800A01F8)--&gt;对象不能安全创建<br />MicrosoftVBScript 运行时错误(0x800A01F9)--&gt;无效的或无资格的引用<br />MicrosoftVBScript 运行时错误(0x800A01FA)--&gt;类没有被定义<br />MicrosoftVBScript 运行时错误(0x800A01FB)--&gt;出现一个意外错误<br />MicrosoftVBScript 运行时错误(0x800A1398)--&gt;缺少常规表达式对象<br />MicrosoftVBScript 运行时错误(0x800A1399)--&gt;常规表达式语法错误<br />MicrosoftVBScript 运行时错误(0x800A139A)--&gt;错误的数量词<br />MicrosoftVBScript 运行时错误(0x800A139B)--&gt;常规表达式中缺少 ']'<br />MicrosoftVBScript 运行时错误(0x800A139C)--&gt;常规表达式中缺少 ')'<br />MicrosoftVBScript 运行时错误(0x800A139D)--&gt;字符集越界<br />MicrosoftVBScript 运行时错误(0x800A802B)--&gt;未找到元素 <br />ActiveServerPages,ASP0126(0x80004005)--&gt;找不到包含文件<br />MicrosoftOLEDBProviderforODBCDrivers(0x80040E14)--&gt;sql语句出错(字段名错误,或数据类型不匹配)<br />MicrosoftOLEDBProviderforODBCDrivers(0x80040E07)--&gt;sql语句出错(要插入或更新的字段的类型与变量数据类型不匹配)<br />MicrosoftOLEDBProviderforODBCDrivers(0x80040E57)--&gt;sql语句出错(要插入或更新的数据溢出)<br />MicrosoftOLEDBProviderforODBCDrivers(0x80040E10)--&gt;sql语句出错(update字段名或要更新的数据类型错误)<br />MicrosoftOLEDBProviderforODBCDrivers(0x80004005)--&gt;sql语句出错(要插入或更新的字段的数值不能为空值)<br />MicrosoftOLEDBProviderforODBCDrivers(0x80004005)--&gt;打开数据库出错，没有在指定目录发现数据库<br />MicrosoftOLEDBProviderforODBCDrivers(0x80040E37)--&gt;没有发现表<br />ODBCDrivers(0x80040E21)--&gt;sql语句出错(数据类型不匹配或表/字段错误或处于编辑状态，或不存在于conn打开的数据库中)<br />ADODB.Recordset(0x800A0BB9)--&gt;sql语句出错(sql语句或conn语句未定义或对一个rs属性进行赋值时发生错误)<br />ADODB.Recordset(0x800A0CC1)--&gt;rs对像出错(rs对像本身不存在或错误地引用了一个不存在的字段名)<br />ADODB.Recordset(0x800A0BCD)--&gt;rs对像出错(记录集中没有记录却对记录集进行操作)<br />ADODB.Recordset(0x800A0E78)--&gt;rs对像出错(记录集不存在,缺少rs.open语句)<br />ADODB.Recordset(0x800A0CC1)--&gt;rs对像出错(引用了一个不存在的字段名)<br />ADODB.Recordset(0x800A0E7D)--&gt;conn定义错误<br />ADODB.Recordset(0x800A0CB3)--&gt;数据库以只读方式打开，无法更新数据 <br />ADODB.Recordset(0x800A000D)--&gt;错误引用rs变量(rs对像已关闭或未定义)</p>]]></description><category>其他</category><comments>http://www.dazix.cn/post/35.html#comment</comments><wfw:comment>http://www.dazix.cn/</wfw:comment><wfw:commentRss>http://www.dazix.cn/feed.asp?cmt=35</wfw:commentRss><trackback:ping>http://www.dazix.cn/cmd.asp?act=tb&amp;id=35&amp;key=f62e8a2f</trackback:ping></item><item><title>正则表达式收集</title><author>30538357@qq.com (dazix)</author><link>http://www.dazix.cn/post/13.html</link><pubDate>Wed, 01 Apr 2009 13:49:58 +0800</pubDate><guid>http://www.dazix.cn/post/13.html</guid><description><![CDATA[<p><strong>限制长度<br /></strong>只能输入n个字符</p><p>表达式:^.{n}$&nbsp;&nbsp; 如^.{4}$<br />描述:只能输入n个字符(空格、汉字、特殊字符等都按1个字符计)<br />-------------------------------------------------------------------------<br /><strong>字符串有效长度</strong></p><p>表达式:^.{1,50}$&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />描述:字符串有效长度(空格、汉字、特殊字符等都按1个字符计)<br />-----------------------------------------------------------------------<br />////////////////////////////////////////////////////////////////<br />//<strong>验证数字</strong><br />////////////////////////////////////////////////////////////////<br />------------------------------------------------------------<br /><strong>只能输入1位数字</strong></p><p>表达式:^\d$&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />描述:匹配1位数字<br />匹配的:0,1,2,3<br />不匹配的:E,22<br />----------------------------------------------------------<br /><strong>只能输入n位数字</strong></p><p>表达式:^\d{n}$&nbsp; 如^\d{8}$&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />描述:匹配8个数字<br />匹配的:12345678,22223334,12344321<br />不匹配的:E,22<br />-------------------------------------------------------------<br /><strong>只能输入至少n个数字</strong></p><p>表达式:^\d{n,}$ 如^\d{8,}$&nbsp;&nbsp;&nbsp; <br />描述:匹配最少n位数字&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />匹配的:12345678,1234567,123123<br />------------------------------------------------------------------<br /><strong>只能输入m到n个数字</strong></p><p>表达式:^\d{m,n}$ 如^\d{7,8}$&nbsp; <br />描述:匹配m到n个数字 <br />匹配的:12345678,1234567<br />不匹配的:123456,123456789<br />------------------------------------------------------------------<br /><strong>只能输入某个区间数字</strong></p><p>表达式:^[12-15]$&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />描述:只能输入某个区间数字&nbsp;&nbsp;&nbsp;&nbsp; <br />匹配的:12,13,14,15<br />不匹配的:11,16<br />----------------------------------------------------------------------<br /><strong>只能输入0和非0打头的数字</strong></p><p>表达式:^(0|[1-9][0-9]*)$&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />描述:只能输入0和非0打头的数字 <br />匹配的:12,10,101,100 <br />不匹配的:01<br />--------------------------------------------------------------------<br />^[0-9]*$&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br /><strong>只能输入数字(任意数字)</strong><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />^\d+$&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br /><strong>非负整数（正整数 + 0）</strong></p><p>^\+?[1-9][0-9]*$&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br /><strong>正整数</strong></p><p>^[0-9]*[1-9][0-9]*$&quot;&nbsp; <br /><strong>正整数</strong></p><p>^((-\d+)|(0+))$&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br /><strong>非正整数（负整数 + 0）</strong></p><p>^\-[1-9][0-9]*$&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br /><strong>负整数</strong></p><p>^-[0-9]*[1-9][0-9]*$&quot; <br /><strong>负整数</strong></p><p>^-?\d+$&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br /><strong>整数</strong></p><p>^\d+(\.\d+)?$&quot; <br /><strong>非负浮点数（正浮点数 + 0）</strong></p><p>^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$&quot; <br /><strong>正浮点数</strong></p><p>^((-\d+(\.\d+)?)|(0+(\.0+)?))$&quot; <br /><strong>非正浮点数（负浮点数 + 0）</strong></p><p>^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$&quot; <br /><strong>负浮点数</strong></p><p>^(-?\d+)(\.\d+)?$&quot; <br /><strong>浮点数 <br /></strong>------------------------------------------------------------------------------------<br /><strong>实数</strong></p><p>表达式:^[-+]?\d+(\.\d+)?$ <br />描述:实数&nbsp;&nbsp;&nbsp; <br />匹配的:18,+3.14,-9.90 <br />不匹配的:.6,33s,67-99<br />--------------------------------------------------------------------<br /><strong>只能输入n位小数的正实数</strong></p><p>表达式:^[0-9]+(.[0-9]{n})?$&nbsp; 如^[0-9]+(.[0-9]{2})?$ <br />描述:只能输入n位小数的正实数&nbsp; <br />匹配的:2.22 <br />不匹配的:2.222<br />--------------------------------------------------------------------<br /><strong>只能输入m-n位小数的正实数</strong></p><p>表达式:^[0-9]+(.[0-9]{m,n})?$ 如^[0-9]+(.[0-9]{1,2})?$ <br />描述:只能输入m-n位小数的正实数 <br />匹配的:2.22,2.2 <br />不匹配的:2.222,-2.2222<br />--------------------------------------------------------------<br />(^\d*\.?\d*[1-9]+\d*$)|(^[1-9]+\d*\.\d*$)&nbsp; <br /><strong>大于零的Decimal数字</strong></p><p>^(\d|-)?(\d|,)*\.?\d*$&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br /><strong>任何Decimal数字纯数字 负数 逗号分割的数字</strong> 点分割的decimal格式 如5,000 -5,000 100.044 .2</p><p>^(\d|-)?(\d|,)*\.?\d*$&nbsp; <br /><strong>0-99999999的带或不带逗号的数字</strong><br />/////////////////////////////////////////////////////////////////////////<br />//<strong>验证西文字符</strong><br />////////////////////////////////////////////////////////////////////////<br />^[A-Za-z]+$&quot;&nbsp;&nbsp;&nbsp;&nbsp; <br /><strong>由26个英文字母组成的字符串</strong></p><p>^[A-Z]+$&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br /><strong>由26个英文字母的大写组成的字符串</strong></p><p>^[a-z]+$&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br /><strong>由26个英文字母的小写组成的字符串</strong></p><p>^[A-Za-z0-9]+$&quot;&nbsp; <br /><strong>由数字和26个英文字母组成的字符串</strong><br />&nbsp;<br />^\w+$&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br /><strong>由数字、26个英文字母或者下划线组成的字符串</strong></p><p>^.[a-zA-Z]\w{m,n}$&nbsp; <br /><strong>匹配英文字符开头的m-n位字符且只能数字字母或下划线</strong></p><p>\b[^\Wa-z0-9_][^\WA-Z0-9_]*\b&nbsp;&nbsp; <br /><strong>首字母只能大写</strong><br />----------------------------------------------------------------------<br /><strong>连在一起的两个相同的单词</strong></p><p>表达式:(\w+)\s+\1<br />描述:验证连在一起的两个相同的单词<br />匹配的:abc abc<br />不匹配的:abc abcd<br />-----------------------------------------------------<br /><strong>双引号括起来的词</strong></p><p>表达式:&quot;((\\&quot;)|[^&quot;(\\&quot;)])+&quot;<br />描述:验证用双引号括起来的词<br />匹配的:<br />&quot;Abc&quot;<br />&quot;abc&quot;sff&quot;<br />不匹配的:<br />&quot;sdfsdfsdf<br />-------------------------------------------------------<br />////////////////////////////////////////////////////<br />//<strong>验证特定格式</strong><br />////////////////////////////////////////////////////<br />-----------------------------------------------------<br /><strong>Email地址</strong></p><p>表达式:^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />描述:普通验证</p><p>表达式:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />描述:微软Email验证</p><p>复杂表达式:^(([^&lt;&gt;;()[\]\\.,;:@&quot;]+(\.[^&lt;&gt;()[\]\\.,;:@&quot;]+)*)|(&quot;.+&quot;))@((([a-z]([-a-z0-9]*[a-z0-9])?)|(#[0-9]+)|(\[((([01]?[0-9]{0,2})|(2(([0-4][0-9])|(5[0-5]))))\.){3}(([01]?[0-9]{0,2})|(2(([0-4][0-9])|(5[0-5]))))\]))\.)*(([a-z]([-a-z0-9]*[a-z0-9])?)|(#[0-9]+)|(\[((([01]?[0-9]{0,2})|(2(([0-4][0-9])|(5[0-5]))))\.){3}(([01]?[0-9]{0,2})|(2(([0-4][0-9])|(5[0-5]))))\]))$ <br />描述:标准验证电子邮件地址,所有符合RFC821(http://www.cis.ohio-state.edu/cgi-bin/rfc/rfc0821.html#page-30)规定的格式的邮件地址<br />-------------------------------------------------------------------------------------------------<br /><strong>Url验证</strong></p><p>表达式:^[a-zA-z]+://(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$&quot;&nbsp; <br />描述:普通Url验证</p><p>表达式:http://([\w-]+\.)+[\w-]+(/[\w- ./?%&amp;=]*)?&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />描述:微软Url验证<br />----------------------------------------------------------------------------------------------<br />表达式:[0-9]{5,9}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />描述:<strong>5-9位的QQ号</strong></p><p>表达式:\d{18}|\d{15}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />描述:<strong>15位18位身份证号</strong></p><p>表达式:^[1-9]([0-9]{16}|[0-9]{13})[xX0-9]$&nbsp;&nbsp; <br />描述:<strong>15或者18位的身份证号，支持带X的</strong></p><p>表达式:^13[0-9]{1}[0-9]{8}|^15[9]{1}[0-9]{8} <br />描述:<strong>包含159的手机号130-139</strong></p><p>表达式:(P\d{7})|G\d{8})&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />描述:<strong>验证P+7个数字和G+8个数字&nbsp; 护照</strong></p><p>表达式:^[a-zA-Z0-9]+([a-zA-Z0-9\-\.]+)?\.(com|org|net|cn|com.cn|edu.cn|grv.cn|)$&nbsp; <br />描述:<strong>验证域名</strong></p><p>表达式:^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$&quot; <br />描述:<strong>IP地址</strong></p><p>表达式:^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$&nbsp; <br />描述:<strong>验证IP</strong><br />--------------------------------------------------------------------------------------<br />信用卡<br />表达式:^[{|\(]?[0-9a-fA-F]{8}[-]?([0-9a-fA-F]{4}[-]?){3}[0-9a-fA-F]{12}[\)|}]?$ <br />描述:<strong>16位数字用连字符或者空格或者分割</strong><br />匹配的:<br />1234343425262837<br />1111-2323-2312-3434<br />1111 2323 2312 3434<br />不匹配的:<br />1111 2323 2312-3434</p><p>表达式:^((?:4\d{3})|(?:5[1-5]\d{2})|(?:6011)|(?:3[68]\d{2})|(?:30[012345]\d))[ -]?(\d{4})[ -]?(\d{4})[ -]?(\d{4}|3[4,7]\d{13})$&nbsp; <br />描述:<strong>验证VISA卡,万事达卡,Discover卡,美国运通卡 <br /></strong>--------------------------------------------------------------------------------------------</p><p>^(\d[- ]*){9}[\dxX]$&nbsp;&nbsp; //验证ISBN国际标准书号&nbsp; 如7-111-19947-2<br />^[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}$ //验证GUID全球唯一标识符 如2064d355-c0b9-41d8-9ef7-9d8b26524751<br />^([a-zA-Z]\:|\\)\\([^\\]+\\)*[^\/:*?&quot;&lt;&gt;|]+\.txt(l)?$ //检查路径和文件扩展名&nbsp; E:\mo.txt 错 E:\ , mo.doc, E:\mo.doc</p><p>图片 src[^&gt;]*[^/].(?:jpg|bmp|gif)(?:\&quot;|\')</p><p>网址 &quot;\&lt;a.+?href=['&quot;&quot;](?!http\:\/\/)(?!mailto\:)(?&gt;foundAnchor&gt;[^'&quot;&quot;&gt;]+?)[^&gt;]*?\&gt;&quot;</p><p>///////////////////////////////////////////////////////////////<br />//<strong>验证中文字符</strong><br />//////////////////////////////////////////////////////////////<br />-------------------------------------------------------------------<br />表达式:^([\u4e00-\u9fa5]+|[a-zA-Z0-9]+)$ <br />描述:<strong>中文</strong></p><p>表达式:[\u4e00-\u9fa5]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />描述:<strong>中文字符</strong></p><p>表达式:(/[^\u4E00-\u9FA5]/g&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />描述:<strong>中文字符</strong></p><p>表达式:^[\u4e00-\u9fa5]{0,}$&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />描述:<strong>只能汉字</strong></p><p>表达式:[^\x00-\xff]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />描述:<strong>双字节字符(包括汉字在内)</strong></p><p>表达式:\n[\s| ]*\r&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />描述:<strong>匹配空行</strong></p><p>表达式:(^\s*)|(\s*$)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />描述:<strong>匹配首尾空格：（像vbscript那样的trim函数） <br /></strong>------------------------------------------------------------<br /><strong>验证16进制颜色值</strong></p><p>表达式:^#?([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?$&nbsp; <br />描述:验证16进制的颜色值,#是可选的。 <br />匹配的:<br />#00ccff<br />ffffcc<br />不匹配的:<br />blue<br />0x000000<br />-------------------------------------------------------------<br />///////////////////////////////////////////////////////<br />//<strong>标记相关</strong><br />///////////////////////////////////////////////////////<br />-------------------------------------------------------<br /><strong>验证HTML标记</strong></p><p>表达式:/&lt;(.*)&gt;.*&lt;\/\1&gt;|&lt;(.*) \/&gt;/<br />描述:匹配HTML标记<br />------------------------------------------------------<br /><strong>验证标记</strong></p><p>描述: 所有的html和xml标记<br />表达式:&lt;[^&gt;]+&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />--------------------------------------------------------------<br /><strong>验证一对封闭的&lt;&gt;标记</strong></p><p>表达式:^#?([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?$<br />描述:验证一对括起来的&lt;&gt;标记。 <br />匹配的:<br />&lt;body&gt; text&lt;br/&gt;More Text &lt;/body&gt;<br />&lt;a href=&quot;link.html&quot;&gt;Link&lt;/a&gt;<br />不匹配的:<br />blue<br />0x000000<br />------------------------------------------------------------<br /><strong>验证HTML中所有合法的on事件</strong></p><p>表达式:(?i:on(blur|c(hange|lick)|dblclick|focus|keypress|(key|mouse)(down|up)|(un)?load|mouse(move|o(ut|ver))|reset|s(elect|submit)))<br />描述:验证HTML中所有合法的on事件<br />匹配的:onclick onmouseover<br />不匹配的:Click Move<br />--------------------------------------------------------<br /><strong>查找html中的注释</strong></p><p>表达式:&lt;!\-\-.*?\-\-&gt;<br />描述:查找html中的注释<br />匹配的:&lt;!--&lt;h1&gt;this text has been removed&lt;/h1&gt;--&gt;<br />不匹配的:&lt;h1&gt;this text has been removed&lt;/h1&gt;<br />----------------------------------------------------------------<br /><strong>查找html中的特定文件（swf.jpg.gif&hellip;）</strong></p><p>表达式:&lt;[^&gt;]*\n?.*=(&quot;|')?(.*\.jpg)(&quot;|')?.*\n?[^&lt;]*&gt;<br />描述:查找html中的特定文件（swf.jpg.gif&hellip;）把jpg换为gif，即是查找所有的gif文件。<br />匹配的:&lt;td background=&quot;../img/img.jpg&quot; &gt;<br />不匹配的:= img.jpg<br />-------------------------------------------------------------------------<br />///////////////////////////////////////////<br />//<strong>日期和时间相关</strong><br />///////////////////////////////////////////<br />-------------------------------------------------------------------------<br />^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //日期格式2007-07-07<br />/^(d{2}|d{4})-((0([1-9]{1}))|(1[1|2]))-(([0-2]([1-9]{1}))|(3[0|1]))$/&nbsp;&nbsp; // 年-月-日 <br />/^((0([1-9]{1}))|(1[1|2]))/(([0-2]([1-9]{1}))|(3[0|1]))/(d{2}|d{4})$/&nbsp;&nbsp; // 月/日/年&nbsp;</p><p>YYYY-MM-DD基本上把闰年和2月等的情况都考虑进去了 <br />^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$<br />---------------------------------------------------------------<br /><strong>日期验证</strong></p><p>表达式:<br />&nbsp;^(?:(?:(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(\/|-|\.)(?:0?2\1(?:29)))|(?:(?:(?:1[6-9]|[2-9]\d)?\d{2})(\/|-|\.)(?:(?:(?:0?[13578]|1[02])\2(?:31))|(?:(?:0?[1,3-9]|1[0-2])\2(29|30))|(?:(?:0?[1-9])|(?:1[0-2]))\2(?:0?[1-9]|1\d|2[0-8]))))$<br />描述:<br />验证格式为y/m/d的日期从1600/1/1 - 9999/12/31的日期<br />匹配的:<br />04/2/29<br />2002-4-30<br />02.10.31<br />不匹配的:<br />2003/2/29<br />02.4.31<br />00/00/00<br />-------------------------------------------------------<br /><strong>合法的日期和时间</strong></p><p>表达式:<br />^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\s(((0?[0-9])|([1-2][0-3]))\:([0-5]?[0-9])((\s)|(\:([0-5]?[0-9])))))?$<br />描述:验证所有合法的日期和时间<br />匹配的:<br />yyyy-MM-dd<br />hh:mm:ss<br />yyyy-MM-dd hh:mm:ss<br />不匹配的:2003/2/29 00/00/00<br />--------------------------------------------------<br /><strong>标准ANSI SQL日期验证</strong></p><p>表达式:<br />^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\s(((0?[1-9])|(1[0-2]))\:([0-5][0-9])((\s)|(\:([0-5][0-9])\s))([AM|PM|am|pm]{2,2})))?$<br />描述:<br />匹配ANSI SQL的日期格式：yyyy-MM-dd hh:mm:ss am/pm 包括检查从1901-2099是否是闰年。<br />匹配的:<br />2004-2-29<br />2004-02-29 10:29:39 pm<br />2004/12/31<br />不匹配的<br />04-2-29<br />04-02-29 10:29:39 pm<br />04/12/31<br />---------------------------------------------------------<br />//////////////////////////////////////////////////////<br />//<strong>其他</strong><br />//////////////////////////////////////////////////////<br />------------------------------------------------------------<br /><strong>匹配字体</strong></p><p>表达式:^(\d)(\d)*( )*(px|PX|Px|pX|pt|PT|Pt|pT|)$<br />描述:查找字体的后缀<br />匹配的:<br />1px<br />100 PT<br />20Px<br />不匹配的:1abc、px、1、sdfs<br />-------------------------------------------------------------<br /><strong>匹配MD5哈西字符串</strong></p><p>表达式:^([a-z0-9]{32})$<br />描述:匹配MD5哈西字符串<br />匹配的:790d2cf6ada1937726c17f1ef41ab125<br />不匹配的:790D2CF6ADA1937726C17F1EF41AB125<br />--------------------------------------------------------------------------- <br />以下是例子：</p><p>利用正则表达式限制网页表单里的文本框输入内容：</p><p>用正则表达式<strong>限制只能输入中文</strong>：onkeyup=&quot;value=value.replace(/[^\u4E00-\u9FA5]/g,'')&quot; onbeforepaste=&quot;clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))&quot;</p><p>1.用正则表达式<strong>限制只能输入全角字符</strong>： onkeyup=&quot;value=value.replace(/[^\uFF00-\uFFFF]/g,'')&quot; onbeforepaste=&quot;clipboardData.setData('text',clipboardData.getData('text').replace(/[^\uFF00-\uFFFF]/g,''))&quot;</p><p>2.用正则表达式<strong>限制只能输入数字</strong>：onkeyup=&quot;value=value.replace(/[^\d]/g,'') &quot;onbeforepaste=&quot;clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))&quot;</p><p>3.用正则表达式<strong>限制只能输入数字和英文</strong>：onkeyup=&quot;value=value.replace(/[\W]/g,'') &quot;onbeforepaste=&quot;clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))&quot;</p><p>4.<strong>计算字符串的长度</strong>（一个双字节字符长度计2，ASCII字符计1）</p><p>String.prototype.len=function(){return this.replace([^\x00-\xff]/g,&quot;aa&quot;).length;}</p><p>5.javascript中没有像vbscript那样的trim函数，我们就可以利用这个表达式来实现，如下：</p><p>String.prototype.trim = function() <br />{ <br />return this.replace(/(^\s*)|(\s*$)/g, &quot;&quot;); <br />}</p><p><strong>利用正则表达式分解和转换IP地址</strong>：</p><p>6.下面是利用正则表达式匹配IP地址，并将IP地址转换成对应数值的Javascript程序：</p><p>function IP2V(ip) <br />{ <br />&nbsp;re=/(\d+)\.(\d+)\.(\d+)\.(\d+)/g //匹配IP地址的正则表达式 <br />&nbsp;if(re.test(ip)) <br />&nbsp;{ <br />&nbsp;&nbsp;return RegExp.$1*Math.pow(255,3))+RegExp.$2*Math.pow(255,2))+RegExp.$3*255+RegExp.$4*1 <br />&nbsp;} <br />&nbsp;else <br />&nbsp;{ <br />&nbsp;&nbsp;throw new Error(&quot;不是一个正确的IP地址!&quot;) <br />&nbsp;} <br />}</p><p>不过上面的程序如果不用正则表达式，而直接用split函数来分解可能更简单，程序如下：</p><p>var ip=&quot;10.100.20.168&quot; <br />ip=ip.split(&quot;.&quot;) <br />alert(&quot;IP值是：&quot;+(ip[0]*255*255*255+ip[1]*255*255+ip[2]*255+ip[3]*1))</p><p>(?&lt;=&gt;)[^&gt;]*(?=&lt;)</p><p>&nbsp;</p><p>&nbsp;</p>]]></description><category>其他</category><comments>http://www.dazix.cn/post/13.html#comment</comments><wfw:comment>http://www.dazix.cn/</wfw:comment><wfw:commentRss>http://www.dazix.cn/feed.asp?cmt=13</wfw:commentRss><trackback:ping>http://www.dazix.cn/cmd.asp?act=tb&amp;id=13&amp;key=40c05502</trackback:ping></item></channel></rss>
