»
Tiếng AnhTiếng PhápTiếng Việt

In - Kiểm tra các mẫu thông thường - JavaScriptBank.com

Phiên bản đầy đủ: jsB@nk » Thủ thuật » Kiểm tra các mẫu thông thường
URL: https://www.javascriptbank.com/regular-expressions-taster.html

Kiểm tra các mẫu thông thường © JavaScriptBank.comHiệu ứng giúp bạn học cách kiểm tra">kiểm tra và sử dụng các Regular Pattern. Các mẫu này rất hữu ích cho việc lập trình.

Phiên bản đầy đủ: jsB@nk » Thủ thuật » Kiểm tra các mẫu thông thường
URL: https://www.javascriptbank.com/regular-expressions-taster.html



JavaScript
<script language="javascript"><!--// Created by: Gilbert Hadley :: http://www.web-wise-wizard.com/function regexpDemo(textInput) {  // Remove white space from beginning of string  textInput.value = textInput.value.replace(new RegExp(/^\s+/),"");  // Remove white space from end of string  textInput.value = textInput.value.replace(new RegExp(/\s+$/),"");  switch (regexpDemo.arguments[1]) {    case 2:      // If true, remove 'http://' from front of string      textInput.value = textInput.value.replace(new RegExp(/^http:\/\//i),"");      break;    case 3:      // If true, remove 'www.' from front of string      textInput.value = textInput.value.replace(new RegExp(/^www\./i),"");      break;    case 4:      // If true, remove trailing back slash      textInput.value = textInput.value.replace(new RegExp(/\/$/),"");      break;    case 5:      // Split the string at the first forward slash      textInput.value = textInput.value.split(new RegExp(/\//),1);      break;    case 6:      // Replace all back slashes with forward slashes      textInput.value = textInput.value.replace(new RegExp(/\\/g),"/");      break;    default:      break;  }}function regexpMatch(form,domainName) {  if (domainName.match(/\.com$|\.net$|\.us$|\.co\.uk$/i)) {    form.textInput.value = "true";  } else {    form.textInput.value = "false";  }}//--></script><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->


HTML
<FORM name=demo1 onsubmit="regexpDemo(this.textInput);return false"       action=# method=post><DIV><B><SMALL>1. Remove white space from both ends of a string.</SMALL></B></DIV>      <DIV><INPUT size=60 value="  This string has white space at both ends "       name=textInput> <INPUT type=submit value=Test> <INPUT type=reset value=Again></DIV>      <DIV><TT>string = string.replace(new RegExp(/^\s+/),&quot;&quot;); // START</TT></DIV>      <DIV><TT>string = string.replace(new RegExp(/\s+$/),&quot;&quot;); // END</TT></DIV></FORM><BR><BR>      <FORM name=demo2 onsubmit="regexpDemo(this.textInput,2);return false"       action=# method=post>      <DIV><B><SMALL>2. Remove white space from ends then &#39;http://&#39; from the start.</SMALL></B></DIV>      <DIV><INPUT size=60 value="  http://javascriptsource.com/ "       name=textInput> <INPUT type=submit value=Test> <INPUT type=reset value=Again></DIV>      <DIV><TT>string = string.replace(new RegExp(/^http:\/\//i),&quot;&quot;);</TT></DIV></FORM><BR><BR>      <FORM name=demo3 onsubmit="regexpDemo(this.textInput,3);return false"       action=# method=post>      <DIV><B><SMALL>3. Remove white space from ends then &#39;www.&#39; from the start.</SMALL></B></DIV>      <DIV><INPUT size=60 value="  www.javascriptsource.com/ " name=textInput> <INPUT type=submit value=Test> <INPUT type=reset value=Again></DIV>      <DIV><TT>string = string.replace(new RegExp(/^www\./i),&quot;&quot;);</TT></DIV></FORM><BR><BR>      <FORM name=demo4 onsubmit="regexpDemo(this.textInput,4);return false"       action=# method=post>      <DIV><B><SMALL>4. Remove white space from ends then any trailing forward slash.</SMALL></B></DIV>      <DIV><INPUT size=60 value="  javascriptsource.com/ " name=textInput> <INPUT type=submit value=Test> <INPUT type=reset value=Again></DIV>      <DIV><TT>string = string.replace(new RegExp(/\/$/),&quot;&quot;);</TT></DIV></FORM><BR><BR>      <FORM name=demo5 onsubmit="regexpDemo(this.textInput,5);return false"       action=# method=post>      <DIV><B><SMALL>5. Remove white space from ends then split the string at first forward slash.</SMALL></B></DIV>      <DIV><INPUT size=60 value="  javascriptsource.com/snippets/ "       name=textInput> <INPUT type=submit value=Test> <INPUT type=reset value=Again></DIV>      <DIV><TT>string = string.split(new RegExp(/\//),1);</TT></DIV></FORM><BR><BR>      <FORM name=demo6 onsubmit="regexpDemo(this.textInput,6);return false"       action=# method=post>      <DIV><B><SMALL>6. Remove white space from ends then replace back slashes with forward slashes.</SMALL></B></DIV>      <DIV><INPUT size=60       value="  D:\javascriptsource\htdocs\snippets\index.html " name=textInput> <INPUT type=submit value=Test> <INPUT type=reset value=Again></DIV>      <DIV><TT>string = string.replace(new RegExp(/\\/g),&quot;/&quot;);</TT></DIV></FORM><BR><BR>      <FORM name=demo7 onsubmit="return false" action=# method=post>      <DIV><B><SMALL>7. Test for a match at end of string against a number of different options.</SMALL></B></DIV>      <DIV>&nbsp;</DIV>      <DIV><TT>var domainName =</TT></DIV>      <TABLE cellSpacing=1 cellPadding=0 border=0>        <TBODY>        <TR>          <TD><INPUT onclick=regexpMatch(this.form,this.value) type=radio             value=javascriptsource.com name=domain>           <TT>javascriptsource.com</TT></TD>          <TD><INPUT onclick=regexpMatch(this.form,this.value) type=radio             value=javascriptsource.us name=domain>           <TT>javascriptsource.us</TT></TD></TR>        <TR>          <TD><INPUT onclick=regexpMatch(this.form,this.value) type=radio             value=javascriptsource.web name=domain>           <TT>javascriptsource.web</TT></TD>          <TD><INPUT onclick=regexpMatch(this.form,this.value) type=radio             value=javascriptsource.ca name=domain>           <TT>javascriptsource.ca</TT></TD></TR>        <TR>          <TD><INPUT onclick=regexpMatch(this.form,this.value) type=radio             value=javascriptsource.net name=domain>           <TT>javascriptsource.net</TT></TD>          <TD><INPUT onclick=regexpMatch(this.form,this.value) type=radio             value=javascriptsource.co.uk name=domain>             <TT>javascriptsource.co.uk</TT></TD></TR>        <TR>          <TD><INPUT onclick=regexpMatch(this.form,this.value) type=radio             value=javascriptsource.info name=domain> <TT>javascriptsource.info &nbsp;</TT></TD>          <TD><INPUT onclick=regexpMatch(this.form,this.value) type=radio             value=javascriptsource.au name=domain>           <TT>javascriptsource.au</TT></TD></TR>        <TR>          <TD></TR></TBODY></TABLE><BR><BR>      <TABLE width="75%" align=center>        <TBODY>        <TR>          <TD><TT><PRE>if (domainName.match(/\.com$|\.net$|\.us$|\.co\.uk$/i)) {  return(true);} else {  return(false);}</PRE></TT></TD></TR></TBODY></TABLE>      <DIV><B><SMALL>result ...</SMALL></B></DIV>      <DIV><INPUT size=40 name=textInput></DIV></FORM>      <DIV align=left>      <P><B>Note:</B> <I>In the context of this demonstration the term &#39;white space&#39; includes any non-printing character, that is any character you cannot see. Examples include blank spaces, carriage returns, line feeds, tab characters, etc.</I></P>      <P><B>RegExp:</B> A regular expression object contains the pattern of a regular expression. It has properties and methods for using that regular expression to find and replace matches in strings or to match patterns in strings. JavaScript RegExp can be used in the following JavaScript String Methods: string.match(), string.search(), string.replace() and string.split().</P></DIV>      </form><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->