📄 reg.htm
字号:
<P>应用:计算字符串的长度(一个双字节字符长度计2,ASCII字符计1)</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: teal">String</SPAN><SPAN style="COLOR: gray">.</SPAN><SPAN style="COLOR: blue">prototype</SPAN><SPAN style="COLOR: gray">.</SPAN><SPAN style="COLOR: blue">len</SPAN><SPAN style="COLOR: gray">=</SPAN><SPAN style="COLOR: green">function</SPAN><SPAN style="COLOR: olive">(){</SPAN><SPAN style="COLOR: gray"> </SPAN><SPAN style="COLOR: green">return</SPAN><SPAN style="COLOR: gray"> </SPAN><SPAN style="COLOR: green">this</SPAN><SPAN style="COLOR: gray">.</SPAN><SPAN style="COLOR: blue">replace</SPAN><SPAN style="COLOR: olive">([</SPAN><SPAN style="COLOR: gray">^\</SPAN><SPAN style="COLOR: blue">x00</SPAN><SPAN style="COLOR: gray">-\</SPAN><SPAN style="COLOR: blue">xff</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: #8b0000">/</SPAN><SPAN style="COLOR: red">g,"aa").length; }</SPAN></DIV></DIV>
<P>匹配空行的<a href="http://www.jb51.net/list/list_6_1.htm" target="_blank"><font color=red>正则</font></a>表达式:</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: gray">\</SPAN><SPAN style="COLOR: blue">n</SPAN><SPAN style="COLOR: olive">[</SPAN><SPAN style="COLOR: gray">\</SPAN><SPAN style="COLOR: blue">s</SPAN><SPAN style="COLOR: gray">|</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">*\</SPAN><SPAN style="COLOR: blue">r</SPAN></DIV></DIV>
<P>匹配HTML标记的<a href="http://www.jb51.net/list/list_6_1.htm" target="_blank"><font color=red>正则</font></a>表达式:</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: #8b0000">/</SPAN><SPAN style="COLOR: red"><(.*)>.*<</SPAN><SPAN style="COLOR: navy">\/</SPAN><SPAN style="COLOR: red">\1>|<(.*) </SPAN><SPAN style="COLOR: navy">\/</SPAN><SPAN style="COLOR: red">></SPAN><SPAN style="COLOR: #8b0000">/</SPAN></DIV></DIV>
<P>匹配首尾空格的<a href="http://www.jb51.net/list/list_6_1.htm" target="_blank"><font color=red>正则</font></a>表达式:</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: olive">(</SPAN><SPAN style="COLOR: gray">^\</SPAN><SPAN style="COLOR: blue">s</SPAN><SPAN style="COLOR: gray">*</SPAN><SPAN style="COLOR: olive">)</SPAN><SPAN style="COLOR: gray">|</SPAN><SPAN style="COLOR: olive">(</SPAN><SPAN style="COLOR: gray">\</SPAN><SPAN style="COLOR: blue">s</SPAN><SPAN style="COLOR: gray">*$</SPAN><SPAN style="COLOR: olive">)</SPAN></DIV></DIV>
<P>应用:j avascript中没有像v bscript那样的trim函数,我们就可以利用这个表达式来实现,如下:</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: teal">String</SPAN><SPAN style="COLOR: gray">.</SPAN><SPAN style="COLOR: blue">prototype</SPAN><SPAN style="COLOR: gray">.</SPAN><SPAN style="COLOR: blue">trim</SPAN><SPAN style="COLOR: gray"> = </SPAN><SPAN style="COLOR: green">function</SPAN><SPAN style="COLOR: olive">()</SPAN><SPAN style="COLOR: gray"><BR></SPAN><SPAN style="COLOR: olive">{</SPAN><SPAN style="COLOR: gray"><BR> </SPAN><SPAN style="COLOR: green">return</SPAN><SPAN style="COLOR: gray"> </SPAN><SPAN style="COLOR: green">this</SPAN><SPAN style="COLOR: gray">.</SPAN><SPAN style="COLOR: blue">replace</SPAN><SPAN style="COLOR: olive">(</SPAN><SPAN style="COLOR: #8b0000">/</SPAN><SPAN style="COLOR: red">(^\s*)|(\s*$)</SPAN><SPAN style="COLOR: #8b0000">/g</SPAN><SPAN style="COLOR: gray">, </SPAN><SPAN style="COLOR: #8b0000">""</SPAN><SPAN style="COLOR: olive">)</SPAN><SPAN style="COLOR: gray">;<BR></SPAN><SPAN style="COLOR: olive">}</SPAN></DIV></DIV>
<P>利用<a href="http://www.jb51.net/list/list_6_1.htm" target="_blank"><font color=red>正则</font></a>表达式分解和转换IP地址<BR>下面是利用<a href="http://www.jb51.net/list/list_6_1.htm" target="_blank"><font color=red>正则</font></a>表达式匹配IP地址,并将IP地址转换成对应数值的Javascript程序:</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: green">function</SPAN><SPAN style="COLOR: gray"> </SPAN><SPAN style="COLOR: blue">IP2V</SPAN><SPAN style="COLOR: olive">(</SPAN><SPAN style="COLOR: blue">ip</SPAN><SPAN style="COLOR: olive">)</SPAN><SPAN style="COLOR: gray"><BR></SPAN><SPAN style="COLOR: olive">{</SPAN><SPAN style="COLOR: gray"><BR> </SPAN><SPAN style="COLOR: blue">re</SPAN><SPAN style="COLOR: gray">=</SPAN><SPAN style="COLOR: #8b0000">/</SPAN><SPAN style="COLOR: red">(\d+)\.(\d+)\.(\d+)\.(\d+)</SPAN><SPAN style="COLOR: #8b0000">/g</SPAN><SPAN style="COLOR: gray"> </SPAN><SPAN style="COLOR: #ffa500">//匹配IP地址的<a href="http://www.jb51.net/list/list_6_1.htm" target="_blank"><font color=red>正则</font></a>表达式</SPAN><SPAN style="COLOR: gray"><BR> </SPAN><SPAN style="COLOR: green">if</SPAN><SPAN style="COLOR: olive">(</SPAN><SPAN style="COLOR: blue">re</SPAN><SPAN style="COLOR: gray">.</SPAN><SPAN style="COLOR: blue">test</SPAN><SPAN style="COLOR: olive">(</SPAN><SPAN style="COLOR: blue">ip</SPAN><SPAN style="COLOR: olive">))</SPAN><SPAN style="COLOR: gray"><BR> </SPAN><SPAN style="COLOR: olive">{</SPAN><SPAN style="COLOR: gray"><BR> </SPAN><SPAN style="COLOR: green">return</SPAN><SPAN style="COLOR: gray"> </SPAN><SPAN style="COLOR: teal">RegExp</SPAN><SPAN style="COLOR: gray">.$</SPAN><SPAN style="COLOR: maroon">1</SPAN><SPAN style="COLOR: gray">*</SPAN><SPAN style="COLOR: teal">Math</SPAN><SPAN style="COLOR: gray">.</SPAN><SPAN style="COLOR: blue">pow</SPAN><SPAN style="COLOR: olive">(</SPAN><SPAN style="COLOR: maroon">255</SPAN><SPAN style="COLOR: gray">,</SPAN><SPAN style="COLOR: maroon">3</SPAN><SPAN style="COLOR: olive">)</SPAN><SPAN style="COLOR: gray">)+</SPAN><SPAN style="COLOR: teal">RegExp</SPAN><SPAN style="COLOR: gray">.$</SPAN><SPAN style="COLOR: maroon">2</SPAN><SPAN style="COLOR: gray">*</SPAN><SPAN style="COLOR: teal">Math</SPAN><SPAN style="COLOR: gray">.</SPAN><SPAN style="COLOR: blue">pow</SPAN><SPAN style="COLOR: olive">(</SPAN><SPAN style="COLOR: maroon">255</SPAN><SPAN style="COLOR: gray">,</SPAN><SPAN style="COLOR: maroon">2</SPAN><SPAN style="COLOR: olive">)</SPAN><SPAN style="COLOR: gray">)+</SPAN><SPAN style="COLOR: teal">RegExp</SPAN><SPAN style="COLOR: gray">.$</SPAN><SPAN style="COLOR: maroon">3</SPAN><SPAN style="COLOR: gray">*</SPAN><SPAN style="COLOR: maroon">255</SPAN><SPAN style="COLOR: gray">+</SPAN><SPAN style="COLOR: teal">RegExp</SPAN><SPAN style="COLOR: gray">.$</SPAN><SPAN style="COLOR: maroon">4</SPAN><SPAN style="COLOR: gray">*</SPAN><SPAN style="COLOR: maroon">1</SPAN><SPAN style="COLOR: gray"><BR> </SPAN><SPAN style="COLOR: olive">}</SPAN><SPAN style="COLOR: gray"><BR> </SPAN><SPAN style="COLOR: green">else</SPAN><SPAN style="COLOR: gray"><BR> </SPAN><SPAN style="COLOR: olive">{</SPAN><SPAN style="COLOR: gray"><BR> </SPAN><SPAN style="COLOR: green">throw</SPAN><SPAN style="COLOR: gray"> </SPAN><SPAN style="COLOR: green">new</SPAN><SPAN style="COLOR: gray"> </SPAN><SPAN style="COLOR: blue">Error</SPAN><SPAN style="COLOR: olive">(</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: red">Not a valid IP address!</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: olive">)</SPAN><SPAN style="COLOR: gray"><BR> </SPAN><SPAN style="COLOR: olive">}</SPAN><SPAN style="COLOR: gray"><BR></SPAN><SPAN style="COLOR: olive">}</SPAN></DIV></DIV>
<P>不过上面的程序如果不用<a href="http://www.jb51.net/list/list_6_1.htm" target="_blank"><font color=red>正则</font></a>表达式,而直接用split函数来分解可能更简单,程序如下:</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: green">var</SPAN><SPAN style="COLOR: gray"> </SPAN><SPAN style="COLOR: blue">ip</SPAN><SPAN style="COLOR: gray">=</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: red">10.100.20.168</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: gray"><BR></SPAN><SPAN style="COLOR: blue">ip</SPAN><SPAN style="COLOR: gray">=</SPAN><SPAN style="COLOR: blue">ip</SPAN><SPAN style="COLOR: gray">.</SPAN><SPAN style="COLOR: blue">split</SPAN><SPAN style="COLOR: olive">(</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: red">.</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: olive">)</SPAN><SPAN style="COLOR: gray"><BR></SPAN><SPAN style="COLOR: blue">alert</SPAN><SPAN style="COLOR: olive">(</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: red">IP值是:</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: gray">+</SPAN><SPAN style="COLOR: olive">(</SPAN><SPAN style="COLOR: blue">ip</SPAN><SPAN style="COLOR: olive">[</SPAN><SPAN style="COLOR: maroon">0</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">*</SPAN><SPAN style="COLOR: maroon">255</SPAN><SPAN style="COLOR: gray">*</SPAN><SPAN style="COLOR: maroon">255</SPAN><SPAN style="COLOR: gray">*</SPAN><SPAN style="COLOR: maroon">255</SPAN><SPAN style="COLOR: gray">+</SPAN><SPAN style="COLOR: blue">ip</SPAN><SPAN style="COLOR: olive">[</SPAN><SPAN style="COLOR: maroon">1</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">*</SPAN><SPAN style="COLOR: maroon">255</SPAN><SPAN style="COLOR: gray">*</SPAN><SPAN style="COLOR: maroon">255</SPAN><SPAN style="COLOR: gray">+</SPAN><SPAN style="COLOR: blue">ip</SPAN><SPAN style="COLOR: olive">[</SPAN><SPAN style="COLOR: maroon">2</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">*</SPAN><SPAN style="COLOR: maroon">255</SPAN><SPAN style="COLOR: gray">+</SPAN><SPAN style="COLOR: blue">ip</SPAN><SPAN style="COLOR: olive">[</SPAN><SPAN style="COLOR: maroon">3</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">*</SPAN><SPAN style="COLOR: maroon">1</SPAN><SPAN style="COLOR: olive">))</SPAN></DIV></DIV>
<P>匹配Email地址的<a href="http://www.jb51.net/list/list_6_1.htm" target="_blank"><font color=red>正则</font></a>表达式:</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: gray">\</SPAN><SPAN style="COLOR: blue">w</SPAN><SPAN style="COLOR: gray">+</SPAN><SPAN style="COLOR: olive">([</SPAN><SPAN style="COLOR: gray">-+.</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">\</SPAN><SPAN style="COLOR: blue">w</SPAN><SPAN style="COLOR: gray">+</SPAN><SPAN style="COLOR: olive">)</SPAN><SPAN style="COLOR: gray">*@\</SPAN><SPAN style="COLOR: blue">w</SPAN><SPAN style="COLOR: gray">+</SPAN><SPAN style="COLOR: olive">([</SPAN><SPAN style="COLOR: gray">-.</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">\</SPAN><SPAN style="COLOR: blue">w</SPAN><SPAN style="COLOR: gray">+</SPAN><SPAN style="COLOR: olive">)</SPAN><SPAN style="COLOR: gray">*\.\</SPAN><SPAN style="COLOR: blue">w</SPAN><SPAN style="COLOR: gray">+</SPAN><SPAN style="COLOR: olive">([</SPAN><SPAN style="COLOR: gray">-.</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">\</SPAN><SPAN style="COLOR: blue">w</SPAN><SPAN style="COLOR: gray">+</SPAN><SPAN style="COLOR: olive">)</SPAN><SPAN style="COLOR: gray">*</SPAN></DIV></DIV>
<P>匹配网址URL的<a href="http://www.jb51.net/list/list_6_1.htm" target="_blank"><font color=red>正则</font></a>表达式:</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: blue">http</SPAN><SPAN style="COLOR: gray">:</SPAN><SPAN style="COLOR: #ffa500">//([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?</SPAN></DIV></DIV>
<P>利用<a href="http://www.jb51.net/list/list_6_1.htm" target="_blank"><font color=red>正则</font></a>表达式去除字串中重复的字符的算法程序:[*注:此程序不正确]</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: green">var</SPAN><SPAN style="COLOR: gray"> </SPAN><SPAN style="COLOR: blue">s</SPAN><SPAN style="COLOR: gray">=</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: red">abacabefgeeii</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: gray"><BR></SPAN><SPAN style="COLOR: green">var</SPAN><SPAN style="COLOR: gray"> </SPAN><SPAN style="COLOR: blue">s1</SPAN><SPAN style="COLOR: gray">=</SPAN><SPAN style="COLOR: blue">s</SPAN><SPAN style="COLOR: gray">.</SPAN><SPAN style="COLOR: blue">replace</SPAN><SPAN style="COLOR: olive">(</SPAN><SPAN style="COLOR: #8b0000">/</SPAN><SPAN style="COLOR: red">(.).*\1</SPAN><SPAN style="COLOR: #8b0000">/g</SPAN><SPAN style="COLOR: gray">,</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: red">$1</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: olive">)</SPAN><SPAN style="COLOR: gray"><BR></SPAN><SPAN style="COLOR: green">var</SPAN><SPAN style="COLOR: gray"> </SPAN><SPAN style="COLOR: blue">re</SPAN><SPAN style="COLOR: gray">=</SPAN><SPAN style="COLOR: green">new</SPAN><SPAN style="COLOR: gray"> </SPAN><SPAN style="COLOR: teal">RegExp</SPAN><SPAN style="COLOR: olive">(</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: red">[</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: gray">+</SPAN><SPAN style="COLOR: blue">s1</SPAN><SPAN style="COLOR: gray">+</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: red">]</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: gray">,</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: red">g</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: olive">)</SPAN><SPAN style="COLOR: gray"><BR></SPAN><SPAN style="COLOR: green">var</SPAN><SPAN style="COLOR: gray"> </SPAN><SPAN style="COLOR: blue">s2</SPAN><SPAN style="COLOR: gray">=</SPAN><SPAN style="COLOR: blue">s</SPAN><SPAN style="COLOR: gray">.</SPAN><SPAN style="COLOR: blue">replace</SPAN><SPAN style="COLOR: olive">(</SPAN><SPAN style="COLOR: blue">re</SPAN><SPAN style="COLOR: gray">,</SPAN><SPAN style="COLOR: #8b0000">""</SPAN><SPAN style="COLOR: olive">)</SPAN><SPAN style="COLOR: gray"><BR></SPAN><SPAN style="COLOR: blue">alert</SPAN><SPAN style="COLOR: olive">(</SPAN><SPAN style="COLOR: blue">s1</SPAN><SPAN style="COLOR: gray">+</SPAN><SPAN style="COLOR: blue">s2</SPAN><SPAN style="COLOR: olive">)</SPAN><SPAN style="COLOR: gray"> </SPAN><SPAN style="COLOR: #ffa500">//结果为:abcefgi</SPAN></DIV></DIV>
<P>*注<BR>===============================<BR>如果var s = “abacabefggeeii”<BR>结果就不对了,结果为:abeicfgg<BR><a href="http://www.jb51.net/list/list_6_1.htm" target="_blank"><font color=red>正则</font></a>表达式的能力有限<BR>===============================</P>
<P>我原来在CSDN上发贴寻求一个表达式来实现去除重复字符的方法,最终没有找到,这是我能想到的最简单的实现方法。思路是使用后向引用取出包括重复的字符,再以重复的字符建立第二个表达式,取到不重复的字符,两者串连。这个方法对于字符顺序有要求的字符串可能不适用。</P>
<P>得用<a href="http://www.jb51.net/list/list_6_1.htm" target="_blank"><font color=red>正则</font></a>表达式从URL地址中提取文件名的javascript程序,如下结果为page1</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: blue">s</SPAN><SPAN style="COLOR: gray">=</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: red">http://blog.penner.cn/page1.htm</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: gray"><BR></SPAN><SPAN style="COLOR: blue">s</SPAN><SPAN style="COLOR: gray">=</SPAN><SPAN style="COLOR: blue">s</SPAN><SPAN style="COLOR: gray">.</SPAN><SPAN style="COLOR: blue">replace</SPAN><SPAN style="COLOR: olive">(</SPAN><SPAN style="COLOR: #8b0000">/</SPAN><SPAN style="COLOR: red">(.*</SPAN><SPAN style="COLOR: navy">\/</SPAN><SPAN style="COLOR: red">){ 0, }([^\.]+).*</SPAN><SPAN style="COLOR: #8b0000">/i</SPAN><SPAN style="COLOR: blue">g</SPAN><SPAN style="COLOR: gray">,</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: red">$2</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: olive">)</SPAN><SPAN style="COLOR: gray"><BR></SPAN><SPAN style="COLOR: blue">alert</SPAN><SPAN style="COLOR: olive">(</SPAN><SPAN style="COLOR: blue">s</SPAN><SPAN style="COLOR: olive">)</SPAN></DIV></DIV>
<P>利用<a href="http://www.jb51.net/list/list_6_1.htm" target="_blank"><font color=red>正则</font></a>表达式限制网页表单里的文本框输入内容:</P>
<P>用<a href="http://www.jb51.net/list/list_6_1.htm" target="_blank"><font color=red>正则</font></a>表达式限制只能输入中文:</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: blue">onkeyup</SPAN><SPAN style="COLOR: gray">=</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: red">value=value.replace(/[^\u4E00-\u9FA5]/g,'')</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: gray"> </SPAN><SPAN style="COLOR: blue">onbeforepaste</SPAN><SPAN style="COLOR: gray">=</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: red">clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))</SPAN><SPAN style="COLOR: #8b0000">"</SPAN></DIV></DIV>
<P>用<a href="http://www.jb51.net/list/list_6_1.htm" target="_blank"><font color=red>正则</font></a>表达式限制只能输入全角字符:</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: blue">onkeyup</SPAN><SPAN style="COLOR: gray">=</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: red">value=value.replace(/[^\uFF00-\uFFFF]/g,'')</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: gray"> </SPAN><SPAN style="COLOR: blue">onbeforepaste</SPAN><SPAN style="COLOR: gray">=</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: red">clipboardData.setData('text',clipboardData.getData('text').replace(/[^\uFF00-\uFFFF]/g,''))</SPAN><SPAN style="COLOR: #8b0000">"</SPAN></DIV></DIV>
<P>用<a href="http://www.jb51.net/list/list_6_1.htm" target="_blank"><font color=red>正则</font></a>表达式限制只能输入数字:</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: blue">onkeyup</SPAN><SPAN style="COLOR: gray">=</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: red">value=value.replace(/[^\d]/g,'') </SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: blue">onbeforepaste</SPAN><SPAN style="COLOR: gray">=</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: red">clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))</SPAN><SPAN style="COLOR: #8b0000">"</SPAN></DIV></DIV>
<P>用<a href="http://www.jb51.net/list/list_6_1.htm" target="_blank"><font color=red>正则</font></a>表达式限制只能输入数字和英文:</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: blue">onkeyup</SPAN><SPAN style="COLOR: gray">=</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: red">value=value.replace(/[\W]/g,'') </SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: blue">onbeforepaste</SPAN><SPAN style="COLOR: gray">=</SPAN><SPAN style="COLOR: #8b0000">"</SPAN><SPAN style="COLOR: red">clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))</SPAN><SPAN style="COLOR: #8b0000">"</SPAN></DIV></DIV>
<P>匹配非负整数(正整数 + 0)</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: gray">^\</SPAN><SPAN style="COLOR: blue">d</SPAN><SPAN style="COLOR: gray">+$</SPAN></DIV></DIV>
<P>匹配正整数</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: gray">^</SPAN><SPAN style="COLOR: olive">[</SPAN><SPAN style="COLOR: maroon">0</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: maroon">9</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">*</SPAN><SPAN style="COLOR: olive">[</SPAN><SPAN style="COLOR: maroon">1</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: maroon">9</SPAN><SPAN style="COLOR: olive">][</SPAN><SPAN style="COLOR: maroon">0</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: maroon">9</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">*$</SPAN></DIV></DIV>
<P>匹配非正整数(负整数 + 0)</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: gray">^</SPAN><SPAN style="COLOR: olive">((</SPAN><SPAN style="COLOR: gray">-\</SPAN><SPAN style="COLOR: blue">d</SPAN><SPAN style="COLOR: gray">+</SPAN><SPAN style="COLOR: olive">)</SPAN><SPAN style="COLOR: gray">|</SPAN><SPAN style="COLOR: olive">(</SPAN><SPAN style="COLOR: maroon">0</SPAN><SPAN style="COLOR: gray">+</SPAN><SPAN style="COLOR: olive">))</SPAN><SPAN style="COLOR: gray">$</SPAN></DIV></DIV>
<P>匹配负整数</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: gray">^-</SPAN><SPAN style="COLOR: olive">[</SPAN><SPAN style="COLOR: maroon">0</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: maroon">9</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">*</SPAN><SPAN style="COLOR: olive">[</SPAN><SPAN style="COLOR: maroon">1</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: maroon">9</SPAN><SPAN style="COLOR: olive">][</SPAN><SPAN style="COLOR: maroon">0</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: maroon">9</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">*$</SPAN></DIV></DIV>
<P>匹配整数</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: gray">^-?\</SPAN><SPAN style="COLOR: blue">d</SPAN><SPAN style="COLOR: gray">+$</SPAN></DIV></DIV>
<P>匹配非负浮点数(正浮点数 + 0)</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: gray">^\</SPAN><SPAN style="COLOR: blue">d</SPAN><SPAN style="COLOR: gray">+</SPAN><SPAN style="COLOR: olive">(</SPAN><SPAN style="COLOR: gray">\.\</SPAN><SPAN style="COLOR: blue">d</SPAN><SPAN style="COLOR: gray">+</SPAN><SPAN style="COLOR: olive">)</SPAN><SPAN style="COLOR: gray">?$</SPAN></DIV></DIV>
<P>匹配正浮点数</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: gray">^</SPAN><SPAN style="COLOR: olive">(([</SPAN><SPAN style="COLOR: maroon">0</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: maroon">9</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">+\.</SPAN><SPAN style="COLOR: olive">[</SPAN><SPAN style="COLOR: maroon">0</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: maroon">9</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">*</SPAN><SPAN style="COLOR: olive">[</SPAN><SPAN style="COLOR: maroon">1</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: maroon">9</SPAN><SPAN style="COLOR: olive">][</SPAN><SPAN style="COLOR: maroon">0</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: maroon">9</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">*</SPAN><SPAN style="COLOR: olive">)</SPAN><SPAN style="COLOR: gray">|</SPAN><SPAN style="COLOR: olive">([</SPAN><SPAN style="COLOR: maroon">0</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: maroon">9</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">*</SPAN><SPAN style="COLOR: olive">[</SPAN><SPAN style="COLOR: maroon">1</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: maroon">9</SPAN><SPAN style="COLOR: olive">][</SPAN><SPAN style="COLOR: maroon">0</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: maroon">9</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">*\.</SPAN><SPAN style="COLOR: olive">[</SPAN><SPAN style="COLOR: maroon">0</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: maroon">9</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">+</SPAN><SPAN style="COLOR: olive">)</SPAN><SPAN style="COLOR: gray">|</SPAN><SPAN style="COLOR: olive">([</SPAN><SPAN style="COLOR: maroon">0</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: maroon">9</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">*</SPAN><SPAN style="COLOR: olive">[</SPAN><SPAN style="COLOR: maroon">1</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: maroon">9</SPAN><SPAN style="COLOR: olive">][</SPAN><SPAN style="COLOR: maroon">0</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: maroon">9</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">*</SPAN><SPAN style="COLOR: olive">))</SPAN><SPAN style="COLOR: gray">$</SPAN></DIV></DIV>
<P>匹配非正浮点数(负浮点数 + 0)</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: gray">^</SPAN><SPAN style="COLOR: olive">((</SPAN><SPAN style="COLOR: gray">-\</SPAN><SPAN style="COLOR: blue">d</SPAN><SPAN style="COLOR: gray">+</SPAN><SPAN style="COLOR: olive">(</SPAN><SPAN style="COLOR: gray">\.\</SPAN><SPAN style="COLOR: blue">d</SPAN><SPAN style="COLOR: gray">+</SPAN><SPAN style="COLOR: olive">)</SPAN><SPAN style="COLOR: gray">?</SPAN><SPAN style="COLOR: olive">)</SPAN><SPAN style="COLOR: gray">|</SPAN><SPAN style="COLOR: olive">(</SPAN><SPAN style="COLOR: maroon">0</SPAN><SPAN style="COLOR: gray">+</SPAN><SPAN style="COLOR: olive">(</SPAN><SPAN style="COLOR: gray">\</SPAN><SPAN style="COLOR: maroon">.0</SPAN><SPAN style="COLOR: gray">+</SPAN><SPAN style="COLOR: olive">)</SPAN><SPAN style="COLOR: gray">?</SPAN><SPAN style="COLOR: olive">))</SPAN><SPAN style="COLOR: gray">$</SPAN></DIV></DIV>
<P>匹配负浮点数</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: gray">^</SPAN><SPAN style="COLOR: olive">(</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: olive">(([</SPAN><SPAN style="COLOR: maroon">0</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: maroon">9</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">+\.</SPAN><SPAN style="COLOR: olive">[</SPAN><SPAN style="COLOR: maroon">0</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: maroon">9</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">*</SPAN><SPAN style="COLOR: olive">[</SPAN><SPAN style="COLOR: maroon">1</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: maroon">9</SPAN><SPAN style="COLOR: olive">][</SPAN><SPAN style="COLOR: maroon">0</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: maroon">9</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">*</SPAN><SPAN style="COLOR: olive">)</SPAN><SPAN style="COLOR: gray">|</SPAN><SPAN style="COLOR: olive">([</SPAN><SPAN style="COLOR: maroon">0</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: maroon">9</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">*</SPAN><SPAN style="COLOR: olive">[</SPAN><SPAN style="COLOR: maroon">1</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: maroon">9</SPAN><SPAN style="COLOR: olive">][</SPAN><SPAN style="COLOR: maroon">0</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: maroon">9</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">*\.</SPAN><SPAN style="COLOR: olive">[</SPAN><SPAN style="COLOR: maroon">0</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: maroon">9</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">+</SPAN><SPAN style="COLOR: olive">)</SPAN><SPAN style="COLOR: gray">|</SPAN><SPAN style="COLOR: olive">([</SPAN><SPAN style="COLOR: maroon">0</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: maroon">9</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">*</SPAN><SPAN style="COLOR: olive">[</SPAN><SPAN style="COLOR: maroon">1</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: maroon">9</SPAN><SPAN style="COLOR: olive">][</SPAN><SPAN style="COLOR: maroon">0</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: maroon">9</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">*</SPAN><SPAN style="COLOR: olive">)))</SPAN><SPAN style="COLOR: gray">$</SPAN></DIV></DIV>
<P>匹配浮点数</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: gray">^</SPAN><SPAN style="COLOR: olive">(</SPAN><SPAN style="COLOR: gray">-?\</SPAN><SPAN style="COLOR: blue">d</SPAN><SPAN style="COLOR: gray">+</SPAN><SPAN style="COLOR: olive">)(</SPAN><SPAN style="COLOR: gray">\.\</SPAN><SPAN style="COLOR: blue">d</SPAN><SPAN style="COLOR: gray">+</SPAN><SPAN style="COLOR: olive">)</SPAN><SPAN style="COLOR: gray">?$</SPAN></DIV></DIV>
<P>匹配由26个英文字母组成的字符串</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: gray">^</SPAN><SPAN style="COLOR: olive">[</SPAN><SPAN style="COLOR: blue">A</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: blue">Za</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: blue">z</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">+$</SPAN></DIV></DIV>
<P>匹配由26个英文字母的大写组成的字符串</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: gray">^</SPAN><SPAN style="COLOR: olive">[</SPAN><SPAN style="COLOR: blue">A</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: blue">Z</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">+$</SPAN></DIV></DIV>
<P>匹配由26个英文字母的小写组成的字符串</P>
<DIV class=hl-surround>
<DIV class=hl-main><SPAN style="COLOR: gray">^</SPAN><SPAN style="COLOR: olive">[</SPAN><SPAN style="COLOR: blue">a</SPAN><SPAN style="COLOR: gray">-</SPAN><SPAN style="COLOR: blue">z</SPAN><SPAN style="COLOR: olive">]</SPAN><SPAN style="COLOR: gray">+$</SPAN></DIV></DIV>
<P>匹配由数字和26个英文字母组成的字符串</P>
<DIV class=hl-surround>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -