Working with Regular Expressions

  • Revision slug: JavaScript/Guide/Obsolete_Pages/Working_with_Regular_Expressions
  • Revision title: Working with Regular Expressions
  • Revision id: 118354
  • Created:
  • Creator: Spooty
  • Is current revision? No
  • Comment /* Working With Regular Expressions */ Minor typo fix

Revision Content

Working With Regular Expressions

Regular expressions are used with the RegExp methods test and exec and with the String methods match, replace, search, and split. These methods are explained in detail in the Core JavaScript Reference.

Method Description
exec A RegExp method that executes a search for a match in a string. It returns an array of information.
test A RegExp method that tests for a match in a string. It returns true or false.
match A String method that executes a search for a match in a string. It returns an array of information or null on a mismatch.
search A String method that tests for a match in a string. It returns the index of the match, or -1 if the search fails.
replace A String method that executes a search for a match in a string, and replaces the matched substring with a replacement substring.
split A String method that uses a regular expression or a fixed string to break a string into an array of substrings.

Table 4.2: Methods that use regular expressions

When you want to know whether a pattern is found in a string, use the test or search method; for more information (but slower execution) use the exec or match methods. If you use exec or match and if the match succeeds, these methods return an array and update properties of the associated regular expression object and also of the predefined regular expression object, RegExp. If the match fails, the exec method returns null (which converts to false).

In the following example, the script uses the exec method to find a match in a string.

<SCRIPT type="text/javascript">
  myRe = /d(b+)d/g;
  myArray = myRe.exec("cdbbdbsbz");
</SCRIPT>

If you do not need to access the properties of the regular expression, an alternative way of creating myArray is with this script:

<SCRIPT type="text/javascript">
  myArray = /d(b+)d/g.exec("cdbbdbsbz");
</SCRIPT>

If you want to construct the regular expression from a string, yet another alternative is this script:

<SCRIPT type="text/javascript">
  myRe = new RegExp ("d(b+)d", "g");
  myArray = myRe.exec("cdbbdbsbz");
</SCRIPT>

With these scripts, the match succeeds and returns the array and updates the properties shown in the following table.

Object Property or index Description In this example
myArray   The matched string and all remembered substrings. {{mediawiki.external('\"dbbd\", \"bb\"')}}
  index The 0-based index of the match in the input string. 1
  input The original string. "cdbbdbsbz"
  {{mediawiki.external(0)}} The last matched characters. "dbbd"
myRe lastIndex The index at which to start the next match. (This property is set only if the regular expression uses the g option, described in Executing a Global Search, Ignoring Case, and Considering Multiline Input.) 5
  source The text of the pattern. Updated at the time that the regular expression is created, not executed. "d(b+)d"

Table 4.3: Results of regular expression execution.

As shown in the second form of this example, you can use the a regular expression created with an object initializer without assigning it to a variable. If you do, however, every occurrence is a new regular expression. For this reason, if you use this form without assigning it to a variable, you cannot subsequently access the properties of that regular expression. For example, assume you have this script:

<SCRIPT type="text/javascript">
  myRe = /d(b+)d/g;
  myArray = myRe.exec("cdbbdbsbz");
  document.writeln("The value of lastIndex is " + myRe.lastIndex);
</SCRIPT>

This script displays:

The value of lastIndex is 5

However, if you have this script:

<SCRIPT type="text/javascript">
  myArray = /d(b+)d/g.exec("cdbbdbsbz");
  document.writeln("The value of lastIndex is " + /d(b+)d/g.lastIndex);
</SCRIPT>

It displays:

The value of lastIndex is 0

The occurrences of /d(b+)d/g in the two statements are different regular expression objects and hence have different values for their lastIndex property. If you need to access the properties of a regular expression created with an object initializer, you should first assign it to a variable.

{{template.PreviousNext("Core_JavaScript_1.5_Guide:Writing_a_Regular_Expression_Pattern", "Core_JavaScript_1.5_Guide:Working_with_Regular_Expressions:Using_Parenthesized_Substring_Matches")}}

{{ wiki.languages( { "fr": "fr/Guide_JavaScript_1.5/Travailler_avec_les_expressions_rationnelles", "ja": "ja/Core_JavaScript_1.5_Guide/Working_with_Regular_Expressions", "pl": "pl/Przewodnik_po_j\u0119zyku_JavaScript_1.5/Praca_z_wyra\u017ceniami_regularnymi" } ) }}

Revision Source

<div class="noinclude"></div>
<ul><li> <a href="en/Core_JavaScript_1.5_Guide/Working_with_Regular_Expressions/Executing_a_Global_Search%2c_Ignoring_Case%2c_and_Considering_Multiline_Input">Executing a Global Search, Ignoring Case, and Considering Multiline Input</a>
</li><li> <a href="en/Core_JavaScript_1.5_Guide/Working_with_Regular_Expressions/Using_Parenthesized_Substring_Matches">Using Parenthesized Substring Matches</a>
</li><li> <a href="en/Core_JavaScript_1.5_Guide/Working_with_Regular_Expressions/Examples_of_Regular_Expressions">Examples of Regular Expressions</a>
</li></ul>
<h3 name="Working_With_Regular_Expressions"> Working With Regular Expressions </h3>
<p>Regular expressions are used with the RegExp methods <code>test</code> and <code>exec</code> and with the String methods <code>match</code>, <code>replace</code>, <code>search</code>, and <code>split</code>. These methods are explained in detail in the <a href="en/Core_JavaScript_1.5_Reference">Core JavaScript Reference</a>.
</p>
<table class="fullwidth-table">
<tbody><tr>
<th>Method</th>
<th>Description</th>
</tr>
<tr>
<td><a href="en/Core_JavaScript_1.5_Reference/Objects/RegExp/exec">exec</a></td>
<td>A RegExp method that executes a search for a match in a string. It returns an array of information.  </td>
</tr>
<tr>
<td><a href="en/Core_JavaScript_1.5_Reference/Objects/RegExp/test">test</a>
</td><td>A RegExp method that tests for a match in a string. It returns true or false.  </td>
</tr>
<tr>
<td><a href="en/Core_JavaScript_1.5_Reference/Objects/String/match">match</a>
</td><td>A String method that executes a search for a match in a string. It returns an array of information or null on a mismatch.  </td>
</tr>
<tr>
<td><a href="en/Core_JavaScript_1.5_Reference/Objects/String/search">search</a>
</td><td>A String method that tests for a match in a string. It returns the index of the match, or -1 if the search fails.  </td>
</tr>
<tr>
<td><a href="en/Core_JavaScript_1.5_Reference/Objects/String/replace">replace</a>
</td><td>A String method that executes a search for a match in a string, and replaces the matched substring with a replacement substring.  </td>
</tr>
<tr>
<td><a href="en/Core_JavaScript_1.5_Reference/Objects/String/split">split</a>
</td><td>A String method that uses a regular expression or a fixed string to break a string into an array of substrings.   </td>
</tr>
</tbody></table>
<p><small><b>Table 4.2: Methods that use regular expressions </b></small>
</p><p>When you want to know whether a pattern is found in a string, use the <code>test</code> or <code>search</code>  method; for more information (but slower execution) use the <code>exec</code> or <code>match</code> methods. If you use <code>exec</code> or <code>match</code> and if the match succeeds, these methods return an array and update properties of the associated regular expression object and also of the predefined regular expression object, <code>RegExp</code>. If the match fails, the <code>exec</code> method returns <code>null</code> (which converts to false).
</p><p>In the following example, the script uses the <code>exec</code> method to find a match in a string.
</p>
<pre>&lt;SCRIPT type="text/javascript"&gt;
  myRe = /d(b+)d/g;
  myArray = myRe.exec("cdbbdbsbz");
&lt;/SCRIPT&gt;
</pre>
<p>If you do not need to access the properties of the regular expression, an alternative way of creating <code>myArray</code> is with this script:
</p>
<pre>&lt;SCRIPT type="text/javascript"&gt;
  myArray = /d(b+)d/g.exec("cdbbdbsbz");
&lt;/SCRIPT&gt;
</pre>
<p>If you want to construct the regular expression from a string, yet another alternative is this script:
</p>
<pre>&lt;SCRIPT type="text/javascript"&gt;
  myRe = new RegExp ("d(b+)d", "g");
  myArray = myRe.exec("cdbbdbsbz");
&lt;/SCRIPT&gt;
</pre>
<p>With these scripts, the match succeeds and returns the array and updates the properties shown in the following table.
</p>
<table class="fullwidth-table">
<tbody><tr>
<th>Object</th>
<th>Property or index</th>
<th>Description</th>
<th>In this example</th>
</tr>
<tr>
<td>myArray</td>	
<td> </td>
<td>The matched string and all remembered substrings.  </td>
<td>{{mediawiki.external('\"dbbd\", \"bb\"')}}</td>
</tr>
<tr>
<td> </td>
<td>index</td>
<td>The 0-based index of the match in the input string.  </td>
<td>1  </td>
</tr>
<tr>
<td> </td>
<td>input</td>
<td>The original string.  </td>
<td>"cdbbdbsbz"  </td>
</tr>
<tr>
<td> </td>
<td>{{mediawiki.external(0)}}</td>
<td>The last matched characters.  </td>
<td>"dbbd"  </td>
</tr>
<tr>
<td>myRe 	</td>
<td>lastIndex</td>
<td>The index at which to start the next match. (This property is set only if the regular expression uses the g option, described in <a href="en/Core_JavaScript_1.5_Guide/Working_with_Regular_Expressions/Executing_a_Global_Search%2c_Ignoring_Case%2c_and_Considering_Multiline_Input">Executing a Global Search, Ignoring Case, and Considering Multiline Input</a>.)  </td>
<td>5  </td>
</tr>
<tr>
<td> </td>
<td>source</td>
<td>The text of the pattern. Updated at the time that the regular expression is created, not executed.  </td>
<td>"d(b+)d"</td>
</tr>
</tbody></table>
<p><small><b>Table 4.3: Results of regular expression execution.</b></small>
</p><p>As shown in the second form of this example, you can use the a regular expression created with an object initializer without assigning it to a variable. If you do, however, every occurrence is a new regular expression. For this reason, if you use this form without assigning it to a variable, you cannot subsequently access the properties of that regular expression. For example, assume you have this script:
</p>
<pre>&lt;SCRIPT type="text/javascript"&gt;
  myRe = /d(b+)d/g;
  myArray = myRe.exec("cdbbdbsbz");
  document.writeln("The value of lastIndex is " + myRe.lastIndex);
&lt;/SCRIPT&gt;
</pre>
<p>This script displays:
</p>
<pre>The value of lastIndex is 5
</pre>
<p>However, if you have this script:
</p>
<pre>&lt;SCRIPT type="text/javascript"&gt;
  myArray = /d(b+)d/g.exec("cdbbdbsbz");
  document.writeln("The value of lastIndex is " + /d(b+)d/g.lastIndex);
&lt;/SCRIPT&gt;
</pre>
<p>It displays:
</p>
<pre>The value of lastIndex is 0
</pre>
<p>The occurrences of <code>/d(b+)d/g</code> in the two statements are different regular expression objects and hence have different values for their <code>lastIndex</code> property. If you need to access the properties of a regular expression created with an object initializer, you should first assign it to a variable.
</p>
<div class="noinclude">
<p>{{template.PreviousNext("Core_JavaScript_1.5_Guide:Writing_a_Regular_Expression_Pattern", "Core_JavaScript_1.5_Guide:Working_with_Regular_Expressions:Using_Parenthesized_Substring_Matches")}}
</p>
</div>
{{ wiki.languages( { "fr": "fr/Guide_JavaScript_1.5/Travailler_avec_les_expressions_rationnelles", "ja": "ja/Core_JavaScript_1.5_Guide/Working_with_Regular_Expressions", "pl": "pl/Przewodnik_po_j\u0119zyku_JavaScript_1.5/Praca_z_wyra\u017ceniami_regularnymi" } ) }}
Revert to this revision