Home arrow JavaScript arrow Page 3 - 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 - Matching a Sub-String with its Sub-Sets
(Page 3 of 5 )

Consider the following script:

 

<script type = "text/javascript">

var availableString = "small and medium and big. Another phrase";

var re = /small.*(and).*(medium).*big/;

 

var myArray = re.exec(availableString);

alert(myArray);

</script>

 

The available string is "small and medium and big. Another phrase." The regexp is,

 

/small.*(and).*(medium).*big/;

 

A subset is identified by parentheses. There are two subsets in the regexp, which are (and) and (medium). The entire regexp will match the sub-string "small and medium and big" in the available string. The subset "and," that is, the first "and" in the sub-string will then be matched, and then the subset "medium" in the sub-string will also be matched.

So the first element in the result array will be "small and medium and big" as expected. The next result array element will be "and," which is the first element officially matched to be remembered. The third element in the result array will be "medium" which is the second element officially matched to be remembered.

So to match subsets of a sub-string, put each sub-string between parentheses in the regexp.


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 10 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials