Utilisation des parenthèses de capture

  • Raccourci de la révision : JavaScript//Guide/Travailler_avec_les_expressions_rationnelles/Utilisation_des_parenthèses_de_capture
  • Titre de la révision : Utilisation des parenthèses de capture
  • ID de la révision : 260020
  • Créé :
  • Créateur : Cariboo
  • Version actuelle ? Non
  • Commentaire

Contenu de la révision

Using Parenthesized Substring Matches

Including parentheses in a regular expression pattern causes the corresponding submatch to be remembered. For example, /a(b)c/ matches the characters 'abc' and remembers 'b'. To recall these parenthesized substring matches, use the Array elements {{mediawiki.external(1)}}, ..., {{mediawiki.external('n')}}.

The number of possible parenthesized substrings is unlimited. The returned array holds all that were found. The following examples illustrate how to use parenthesized substring matches.

Example 1.
The following script uses the replace method to switch the words in the string. For the replacement text, the script uses the $1 and $2 in the replacement to denote the first string and second parenthesized substring match.

<SCRIPT LANGUAGE="JavaScript1.2">
re = /(\w+)\s(\w+)/;
str = "John Smith";
newstr = str.replace(re, "$2, $1");
document.write(newstr)
</SCRIPT>

This prints "Smith, John".

Example 2.
In the following example, RegExp.input is set by the Change event. In the getInfo function, the exec method, called using the () shortcut notation, uses the value of RegExp.input as its argument.

<HTML>

<SCRIPT LANGUAGE="JavaScript1.2">
function getInfo(){
   a = /(\w+)\s(\d+)/();
   window.alert(a[1] + ", your age is " + a[2]);
}
</SCRIPT>

Enter your first name and your age, and then press Enter.

<FORM>
<INPUT TYPE="text" NAME="NameAge" onChange="getInfo(this);">
</FORM>

</HTML>

{{template.PreviousNext("Core_JavaScript_1.5_Guide:Working_with_Regular_Expressions", "Core_JavaScript_1.5_Guide:Working_with_Regular_Expressions:Executing_a_Global_Search%2C_Ignoring_Case%2C_and_Considering_Multiline_Input")}}

{{ wiki.languages( { "pl": "pl/Przewodnik_po_j\u0119zyku_JavaScript_1.5/Praca_z_wyra\u017ceniami_regularnymi/U\u017cycie_odpowiedniego_znaku" } ) }}

Source de la révision

<p>
</p>
<h4 name="Using_Parenthesized_Substring_Matches"> Using Parenthesized Substring Matches </h4>
<p>Including parentheses in a regular expression pattern causes the corresponding submatch to be remembered. For example, <code>/a(b)c/</code> matches the characters 'abc' and remembers 'b'. To recall these parenthesized substring matches, use the Array elements {{mediawiki.external(1)}}, ..., {{mediawiki.external('n')}}.
</p><p>The number of possible parenthesized substrings is unlimited. The returned array holds all that were found. The following examples illustrate how to use parenthesized substring matches.
</p><p><b>Example 1.</b><br>
The following script uses the <code>replace</code> method to switch the words in the string. For the replacement text, the script uses the <code>$1</code> and <code>$2</code> in the replacement to denote the first string and second parenthesized substring match.
</p>
<pre>&lt;SCRIPT LANGUAGE="JavaScript1.2"&gt;
re = /(\w+)\s(\w+)/;
str = "John Smith";
newstr = str.replace(re, "$2, $1");
document.write(newstr)
&lt;/SCRIPT&gt;
</pre>
<p>This prints "Smith, John".
</p><p><b>Example 2.</b><br>
In the following example, <code>RegExp.input</code> is set by the <code>Change</code> event. In the <code>getInfo</code> function, the <code>exec</code> method, called using the () shortcut notation, uses the value of <code>RegExp.input</code> as its argument.
</p>
<pre>&lt;HTML&gt;

&lt;SCRIPT LANGUAGE="JavaScript1.2"&gt;
function getInfo(){
   a = /(\w+)\s(\d+)/();
   window.alert(a[1] + ", your age is " + a[2]);
}
&lt;/SCRIPT&gt;

Enter your first name and your age, and then press Enter.

&lt;FORM&gt;
&lt;INPUT TYPE="text" NAME="NameAge" onChange="getInfo(this);"&gt;
&lt;/FORM&gt;

&lt;/HTML&gt;
</pre>
<p>{{template.PreviousNext("Core_JavaScript_1.5_Guide:Working_with_Regular_Expressions", "Core_JavaScript_1.5_Guide:Working_with_Regular_Expressions:Executing_a_Global_Search%2C_Ignoring_Case%2C_and_Considering_Multiline_Input")}}
</p>{{ wiki.languages( { "pl": "pl/Przewodnik_po_j\u0119zyku_JavaScript_1.5/Praca_z_wyra\u017ceniami_regularnymi/U\u017cycie_odpowiedniego_znaku" } ) }}
Revenir à cette révision