📄 js_obj_regexp.asp@output=print
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JavaScript RegExp Object</title>
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Keywords" content="xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql,colors,soap,php,authoring,programming,training,learning,beginner's guide,primer,lessons,school,howto,reference,examples,samples,source code,tags,demos,tips,links,FAQ,tag list,forms,frames,color table,w3c,cascading style sheets,active server pages,dynamic html,internet,database,development,Web building,Webmaster,html guide" />
<meta name="Description" content="Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building." />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "../../https@ssl./default.htm" : "../../www./default.htm");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3855518-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>
</head>
<body>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
<h1>JavaScript RegExp Object</h1>
<a href="js_obj_math.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="js_obj_htmldom.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>
<hr />
<p class="intro">The RegExp object is used to specify what to search for in a
text</p>
<hr />
<h2>What is RegExp</h2>
<p>RegExp, is short for regular expression.</p>
<p>When you search in a text, you can use a pattern to describe what you are
searching for. <b>RegExp IS this pattern</b>.<br />
<br />
A simple pattern can be a single character.</p>
<p>A more complicated pattern consists of more characters, and can be
used for parsing, format checking, substitution and more.</p>
<p>You can specify where in the string to search, what type of characters to search for, and more.</p>
<hr>
<h2>Defining RegExp</h2>
<p>The RegExp object is used to store the search pattern.</p>
<p>We define a RegExp object with the <i>new</i> keyword. The following code line
defines a RegExp object called patt1 with the pattern "e":</p>
<table class="ex" id="table29" border="1" cellspacing="0" width="100%">
<tr>
<td>
<pre>var patt1=new RegExp("e");</pre>
</td>
</tr>
</table>
<p>When you use this RegExp object to search in a string, you will find
the letter "e".
<br />
</p>
<hr />
<h2>Methods of the RegExp Object</h2>
<p>The RegExp Object has 3 methods: test(), exec(), and compile().</p>
<hr />
<h2>test()</h2>
<p>The test() method searches a string for a specified value. Returns true or
false</p>
<h2>Example:</h2>
<table class="ex" id="table12" border="1" cellspacing="0" width="100%">
<tr>
<td>
<pre>var patt1=new RegExp("e");</pre>
<pre>document.write(patt1.test("The best things in life are free"));</pre>
</td>
</tr>
</table>
<p>Since there is an "e" in the string, the output of the code above will be:</p>
<table class="ex" border="1" cellspacing="0" width="100%" id="table15">
<tr>
<td>
<pre>true</pre>
</td>
</tr>
</table>
<p><a target="_blank" href="tryit.asp@filename=tryjs_regexp_test">Try it yourself</a></p>
<hr />
<h2>exec()</h2>
<p>The exec() method searches a string for a specified value. Returns the text
of the found value. If no match is
found, it returns <i>null</i></p>
<h2>Example 1:</h2>
<table class="ex" id="table32" border="1" cellspacing="0" width="100%">
<tr>
<td>
<pre>var patt1=new RegExp("e");</pre>
<pre>document.write(patt1.exec("The best things in life are free"));</pre>
</td>
</tr>
</table>
<p>Since there is an "e" in the string, the output of the code above will be:</p>
<table class="ex" border="1" cellspacing="0" width="100%" id="table33">
<tr>
<td>
<pre>e</pre>
</td>
</tr>
</table>
<p><a target="_blank" href="tryit.asp@filename=tryjs_regexp_exec">Try it yourself</a></p>
<h2>Example 2:</h2>
<p>You can add a second parameter to the RegExp object, to specify your search.
For example; if you want to find all occurrences of a character, you can use the
"g" parameter ("global").</p>
<p>For a complete list of how to modify your search, visit our <a href="../jsref/jsref_obj_regexp.asp">complete
RegExp object reference</a>.</p>
<p>When using the "g" parameter, the exec() method works like this:</p>
<ul>
<li>Finds the first occurence of "e", and stores its position</li>
<li>If you run exec() again, it starts at the stored position, and finds the
next occurence of "e", and stores its position</li>
</ul>
<table class="ex" id="table14" border="1" cellspacing="0" width="100%">
<tr>
<td>
<pre>var patt1=new RegExp("e","g");
do
{
result=patt1.exec("The best things in life are free");
document.write(result);
}
while (result!=null)</pre>
</td>
</tr>
</table>
<p>Since there is six "e" letters in the string, the output of the code above will be:</p>
<table class="ex" border="1" cellspacing="0" width="100%" id="table16">
<tr>
<td>
<pre>eeeeeenull</pre>
</td>
</tr>
</table>
<p><a target="_blank" href="tryit.asp@filename=tryjs_regexp_exec2">Try it yourself</a></p>
<hr />
<h2>compile()</h2>
<p>The compile() method is used to change the RegExp.</p>
<p>compile() can change both the search pattern, and add or remove the second
parameter.</p>
<h2>Example:</h2>
<table class="ex" id="table36" border="1" cellspacing="0" width="100%">
<tr>
<td>
<pre>var patt1=new RegExp("e");</pre>
<pre>document.write(patt1.test("The best things in life are free"));</pre>
<pre>patt1.compile("d");</pre>
<pre>document.write(patt1.test("The best things in life are free"));</pre>
</td>
</tr>
</table>
<p>Since there is an "e" in the string, but not a "d", the output of the code above will be:</p>
<table class="ex" border="1" cellspacing="0" width="100%" id="table37">
<tr>
<td>
<pre>truefalse</pre>
</td>
</tr>
</table>
<p><a target="_blank" href="tryit.asp@filename=tryjs_regexp_compile">Try it
yourself</a></p>
<hr />
<h2>Complete RegExp Object Reference</h2>
<p>
For a complete reference of all the properties and methods that can be used with
the RegExp object, go to our <a href="../jsref/jsref_obj_regexp.asp">complete
RegExp object reference</a>.</p>
<p>The reference contains a brief description and examples of use for each
property and method including the string object<br />
</p>
<hr />
<a href="js_obj_math.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" width="100" height="20" /></a>
<a href="js_obj_htmldom.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -