Home arrow JavaScript arrow Page 6 - Introduction to Regular Expressions in JavaScript
JAVASCRIPT

Introduction to Regular Expressions in JavaScript


What exactly is a regular expression, and how is it handled in JavaScript? These are the questions tackled by this five-part series. In this first part, we'll introduce the topic of regular expressions (often abbreviated RegExp), and delve into objects, patterns, and variables.

Author Info:
By: Chrysanthus Forcha
Rating: 5 stars5 stars5 stars5 stars5 stars / 2
July 08, 2009
TABLE OF CONTENTS:
  1. · Introduction to Regular Expressions in JavaScript
  2. · Simple Word Matching
  3. · Meaning of Pattern
  4. · Regular Expression Object
  5. · Simple Usage of the Literal Text Format and the Constructor Function
  6. · The Flags

print this article
SEARCH DEVARTICLES

Introduction to Regular Expressions in JavaScript - The Flags
(Page 6 of 6 )

You have three possible values:

g : meaning global match.

i : meaning ignore case.

m : meaning match over multiple lines.


The g Flag

Consider the following statement:


var availableString = "I have a dog. You have a dog. She has a dog.";


Matching normally occurs for the first case in an available string. If your regexp is


var re = /dog/


then it is the "dog" in the first sentence that will be matched (seen in the available string). The "dog" in the second and third sentences will not be matched. If you want to match all the cases of the word "dog," then your regexp will have to be


var re = /dog/g


The g is a flag that means global match; that is, match all the occurrences of the regexp in the string. The question you may have now is "What do you gain with global matching?" As we go on, you will see the uses. For example, you may want to know the number of occurrences of the regexp in the available string. I will show you how to get this later.

With the constructor function you would have


var re = new RegExp("dog","g")

 

This statement is equivalent to the previous one.


The i Flag

By default, matching is case sensitive. To make it case insensitive, you have to use the i flag.


So if we have


var re = /send/


which is equivalent to


var re = new RegExg("send")


and then we also have


var availableString = "Click the Send button."


The following code will not produce a match:


<script type="text/javascript">

var re = /send/;

var availableString = "Click the Send button.";

 

if (re.test(availableString))

alert('Matched')

else

alert('Not Matched')

</script>


The regexp did not match the available string because the regexp has "send" where s is in lower case, but the available string has "Send" where S is in upper case. If you want this matching to be case insensitive, then your regexp will have to be


var re = /send/i


or


var re = new RegExg("send","i")


The following code will produce a match.


<script type="text/javascript">

var re = new RegExp("send","i");

var availableString = " Click the Send button.";

 

if (re.test(availableString))

alert('Matched')

else

alert('Not Matched')

</script>


Matching has occurred because we have made the regexp case insensitive, with the i flag.

We take a break here and continue in the next part of the series.


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
JAVASCRIPT ARTICLES

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials