Home arrow JavaScript arrow Page 2 - JavaScript String Regular Expressions
JAVASCRIPT

JavaScript String Regular Expressions


The regular expression object in JavaScript has the test() and exec() methods for regular expression problems. This object is called RegExp. The JavaScript String object has the match(), search(), replace() and split() methods for regular expression problems. The string object methods are actually better and easier to apply than the RegExp object methods. This two-part series takes a look at what you can do with them.

Author Info:
By: Chrysanthus Forcha
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
September 24, 2009
TABLE OF CONTENTS:
  1. · JavaScript String Regular Expressions
  2. · Verification of Match Occurrence
  3. · Knowing Where Matching Occurs in the Available String
  4. · The String Search Method
  5. · Extracting Matches

print this article
SEARCH DEVARTICLES

JavaScript String Regular Expressions - Verification of Match Occurrence
(Page 2 of 5 )

If you just want to know if matching occurs between the RegExp pattern and the available string, then you have to use the test() method of the RegExp object. The following code illustrates this:

<html>


<head>

</head>


<body>

<script type="text/javascript">

var availableString = "The dog is outside the house.";


var re = /dog/

if (re.test(availableString))

alert('Matched')

else

alert('Not Matched')

</script>

</body>


</html>

The available string is "The dog is outside the house." The RegExp pattern is /dog/. The RegExp object is the re variable. The test() method takes the available string as an argument. So in the if-condition we have,

re.test(availableString)

The test() method returns true if a match occurs and false if a match does not occur. In the code, if it returns true, then the JavaScript alert’s box displays “Match.” If it returns false, then the alert box displays “Not Matched.”

Is there an equivalent String method? No. There is no equivalent string method that will simply return true or false. Whenever you just need to know whether a match occurs (true) or not (false), use the test() method of the regular expression object (RexExp).


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