Home arrow JavaScript arrow Replacing and Spliting JavaScript Sub Strings
JAVASCRIPT

Replacing and Spliting JavaScript Sub Strings


In this second part of a two-part series on JavaScript String regular expressions, I look at multiline matching, how to replace matched sub-strings and how to split an available string using a regular expression object. Before we do that, let us look at the String way of extracting matches and then compare it with that of the RegExp object.

Author Info:
By: Chrysanthus Forcha
Rating: 5 stars5 stars5 stars5 stars5 stars / 1
October 01, 2009
TABLE OF CONTENTS:
  1. · Replacing and Spliting JavaScript Sub Strings
  2. · Extracting Continued
  3. · Search and Replace a Matched sub-String
  4. · Splitting a String

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Replacing and Spliting JavaScript Sub Strings
(Page 1 of 4 )

Extracting matches with the String match() method.

Note: The JavaScript String object has the match() method; it is not the RegExp object that has the method. This method returns an array of one element (the first) if it sees a match. If you use a global flag, then the elements in the array will be all the sub-strings found. The way the String match() method is used is straightforward. There is no ambiguity, as we shall see below.

Consider the regexp,

re = /rat/;

Consider the available string,

"A cat is an animal. A rat is an animal. A bat is an animal."

The pattern should match "rat" in the available string. The alert statement in the following code displays a one-element array, with one value, "rat."

<html>


<head>

</head>


<body>

<script type="text/javascript">

var availableString = "A cat is an animal. A rat is an animal. A bat is an animal.";

var re = /cat/;


myArray = availableString.match(re);

alert(myArray);

</script>

</body>


</html>


We have used the string match method in the statement:

myArray = availableString.match(re);

Only one match could occur in the available string; only one element was matched, therefore the returned array has only one element, "rat" as expected. If no matching occurs, null is returned; an array is not returned; the same is true with the exec() method.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- 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
- Dynamic jQuery Styling

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