Home arrow JavaScript arrow Page 5 - String Objects and Regular Expressions in JavaScript
JAVASCRIPT

String Objects and Regular Expressions in JavaScript


Welcome to the fifth article in a five-part series on JavaScript regular expressions. In this article, we begin the second and final phase of the series. In this phase we shall deal more with the properties and methods of the regexp object. We shall also deal with some properties and methods of the string object that are related to regular expressions.

Author Info:
By: Chrysanthus Forcha
Rating: 3 stars3 stars3 stars3 stars3 stars / 2
August 05, 2009
TABLE OF CONTENTS:
  1. · String Objects and Regular Expressions in JavaScript
  2. · Remembering a Match
  3. · Matching a Sub-String with its Sub-Sets
  4. · Search and Replace
  5. · Knowing the Index of the Match

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
String Objects and Regular Expressions in JavaScript - Knowing the Index of the Match
(Page 5 of 5 )

The string object has a method called search. This method looks for a match in an available string, and returns the index of the match if a match occurred, or -1 if the match failed. Index counting in a JavaScript string begins from zero. The index obtained is the position of the first character in a matched sub-string. The syntax is:

 

var returnedIndex = availableString.search(regexp)

 

Note: If the search fails (no match found) the returnedIndex is -1.

 

The following script illustrates the case where the search is successful. Try the code.

 

<script type = "text/javascript">

var availableString = "This is a girl. Another phrase.";

var re = /girl/;

 

var returnedIndex = availableString.search(re);

 

alert(returnedIndex);

</script>

 

The index of the string object obtained here is opposite in nature to the lastIndex of the regexp object, which gives you the position just after the match. The index here gives the position of the first character of the sub-string for the match.

The following script illustrates the case where the search fails. Try the code.

 

<script type = "text/javascript">

var availableString = "This is a girl. Another phrase.";

var re = /boy/;

 

var returnedIndex = availableString.search(re);

 

alert(returnedIndex);

</script>

 

Literal Text Format or the RegExp Constructor Function

The literal notation provides compilation of the regular expression when the expression is evaluated. Use literal notation when the regular expression will remain constant. For example, if you use literal notation to construct a regular expression used in a loop, the regular expression won't be recompiled on each iteration.

The constructor of the regular expression object, for example, new RegExp("ab+c"), provides runtime compilation of the regular expression. Use the constructor function when you know the regular expression pattern will be changing, or you don't know the pattern and are getting it from another source, such as user input.

RegExps and HTML Window

A separate predefined RegExp object is available in each window; that is, each separate thread of JavaScript execution gets its own RegExp object. Because each script runs to completion without interruption in a thread, this assures that different scripts do not overwrite values of the RegExp object.

We have come to the end of the series. I hope you appreciated it.


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

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



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