Advanced Searching With Flags

  • Revision slug: JavaScript/Guide/Obsolete_Pages/Working_with_Regular_Expressions/Advanced_Searching_With_Flags
  • Revision title: Advanced Searching With Flags
  • Revision id: 118319
  • Created:
  • Creator: happysadman
  • Is current revision? No
  • Comment 2 words added

Revision Content

Advanced Searching With Flags

Regular expressions have four optional flags that allow for global and case insensitive searching. To indicate a global search, use the g flag. To indicate a case-insensitive search, use the i flag. To indicate a multi-line search, use the m flag. To perform a "sticky" search, that matches starting at the current position in the target string, use the y flag. These flags can be used separately or together in any order, and are included as part of the regular expression.

{{ Fx_minversion_note("3") }}

To include a flag with the regular expression, use this syntax:

re = /pattern/flags
re = new RegExp("pattern", ["flags"])

Note that the flags are an integral part of a regular expression. They cannot be added or removed later.

For example, re = /\w+\s/g creates a regular expression that looks for one or more characters followed by a space, and it looks for this combination throughout the string.

<script type="text/javascript">
 re = /\w+\s/g;
 str = "fee fi fo fum";
 myArray = str.match(re);
 document.write(myArray);
</script>

This displays {{ mediawiki.external('\"fee \", \"fi \", \"fo \"') }}. In this example, you could replace the line:

re = /\w+\s/g;

with:

re = new RegExp("\\w+\\s", "g");

and get the same result.

The m flag is used to specify that a multiline input string should be treated as multiple lines. If the m flag is used, ^ and $ match at the start or end of any line within the input string instead of the start or end of the entire string.

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

 

 

{{ languages( { "zh-tw": "zh_tw/Core_JavaScript_1.5_教學/正規表達式的運用/使用標誌的進階搜尋", "es": "es/Gu\u00eda_JavaScript_1.5/Trabajar_con_expresiones_regulares/Ejecutar_una_busqueda_global,_discriminar_mayusculas_y_minusculas_y_considerar_entrada_multil\u00ednea", "fr": "fr/Guide_JavaScript_1.5/Travailler_avec_les_expressions_rationnelles/Ex\u00e9cution_de_recherches_globales,_ignorer_la_casse,_utilisation_de_cha\u00eenes_multilignes", "ja": "ja/Core_JavaScript_1.5_Guide/Working_with_Regular_Expressions/Advanced_Searching_With_Flags", "pl": "pl/Przewodnik_po_j\u0119zyku_JavaScript_1.5/Praca_z_wyra\u017ceniami_regularnymi/Globalne_wyszukiwanie,_wielko\u015b\u0107_znak\u00f3w,_wieloliniowe_wej\u015bcie" } ) }}

Revision Source

<h3 name="Advanced_Searching_With_Flags">Advanced Searching With Flags</h3>
<p>Regular expressions have four optional flags that allow for global and case insensitive searching. To indicate a global search, use the <code>g</code> flag. To indicate a case-insensitive search, use the <code>i</code> flag. To indicate a multi-line search, use the <code>m</code> flag. To perform a "sticky" search, that matches starting at the current position in the target string, use the <code>y</code> flag. These flags can be used separately or together in any order, and are included as part of the regular expression.</p>
<p>{{ Fx_minversion_note("3") }}</p>
<p>To include a flag with the regular expression, use this syntax:</p>
<pre>re = /pattern/flags
re = new RegExp("pattern", ["flags"])
</pre>
<p>Note that the flags are an integral part of a regular expression. They cannot be added or removed later.</p>
<p>For example, <code>re = /\w+\s/g</code> creates a regular expression that looks for one or more characters followed by a space, and it looks for this combination throughout the string.</p>
<pre>&lt;script type="text/javascript"&gt;
 re = /\w+\s/g;
 str = "fee fi fo fum";
 myArray = str.match(re);
 document.write(myArray);
&lt;/script&gt;
</pre>
<p>This displays {{ mediawiki.external('\"fee \", \"fi \", \"fo \"') }}. In this example, you could replace the line:</p>
<pre>re = /\w+\s/g;
</pre>
<p>with:</p>
<pre>re = new RegExp("\\w+\\s", "g");
</pre>
<p>and get the same result.</p>
<p>The <code>m</code> flag is used to specify that a multiline input string should be treated as multiple lines. If the <code>m</code> flag is used, ^ and $ match at the start or end of any line within the input string instead of the start or end of the entire string.</p>
<div class="noinclude">
<p>{{ PreviousNext("Core_JavaScript_1.5_Guide:Working_with_Regular_Expressions:Using_Parenthesized_Substring_Matches", "Core_JavaScript_1.5_Guide:Working_with_Regular_Expressions:Examples_of_Regular_Expressions") }}</p>
</div>
<p> </p>
<p> </p>
<p>{{ languages( { "zh-tw": "zh_tw/Core_JavaScript_1.5_教學/正規表達式的運用/使用標誌的進階搜尋", "es": "es/Gu\u00eda_JavaScript_1.5/Trabajar_con_expresiones_regulares/Ejecutar_una_busqueda_global,_discriminar_mayusculas_y_minusculas_y_considerar_entrada_multil\u00ednea", "fr": "fr/Guide_JavaScript_1.5/Travailler_avec_les_expressions_rationnelles/Ex\u00e9cution_de_recherches_globales,_ignorer_la_casse,_utilisation_de_cha\u00eenes_multilignes", "ja": "ja/Core_JavaScript_1.5_Guide/Working_with_Regular_Expressions/Advanced_Searching_With_Flags", "pl": "pl/Przewodnik_po_j\u0119zyku_JavaScript_1.5/Praca_z_wyra\u017ceniami_regularnymi/Globalne_wyszukiwanie,_wielko\u015b\u0107_znak\u00f3w,_wieloliniowe_wej\u015bcie" } ) }}</p>
Revert to this revision