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: 118312
  • Created:
  • Creator: Major Small
  • Is current revision? No
  • Comment more <noinclude>

Revision Content

Executing a Global Search, Ignoring Case, and Considering Multiline Input

Regular expressions have three 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. These flags can be used separately or together in any order, and are included as part of the regular expression.

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.

{{template.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")}}

{{ wiki.languages( { "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/Executing_a_Global_Search,_Ignoring_Case,_and_Considering_Multiline_Input", "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

<div class="noinclude"></div>
<h3 name="Executing_a_Global_Search.2C_Ignoring_Case.2C_and_Considering_Multiline_Input"> Executing a Global Search, Ignoring Case, and Considering Multiline Input </h3>
<p>Regular expressions have three 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. These flags can be used separately or together in any order, and are included as part of the regular expression.
</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>{{template.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>
{{ wiki.languages( { "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/Executing_a_Global_Search,_Ignoring_Case,_and_Considering_Multiline_Input", "pl": "pl/Przewodnik_po_j\u0119zyku_JavaScript_1.5/Praca_z_wyra\u017ceniami_regularnymi/Globalne_wyszukiwanie,_wielko\u015b\u0107_znak\u00f3w,_wieloliniowe_wej\u015bcie" } ) }}
Revert to this revision