Home arrow JavaScript arrow Page 4 - 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 - Search and Replace
(Page 4 of 5 )

You can search (match) a sub-string in an available string, and then replace it with a new sub-string. The syntax is

 

var newAvailableString = availableString.replace(regexp, newSubStr)

 

The method replace() is a method of the string object (availableString). It is not a method of the regular expression object. Regexp, above, is either the variable name of the regular expression object or the literal.

After the substitution, the original available string is not changed. The string returned is the available string with the substituted sub-string. The following script illustrates this.

 

<script type = "text/javascript">

var availableString = "This is a girl";

var re = /girl/;

var newSubStr = "boy";

 

var newAvailableString = availableString.replace(re, newSubStr);

 

alert(availableString);

alert(newAvailableString);

</script>

 

Try the above code.

To replace all possible matches, include the g flag in the regexp. The following script illustrates this:

 

<script type = "text/javascript">

var availableString = "This is a girl. That is a girl.";

var re = /girl/g;

var newSubStr = "boy";

 

var newAvailableString = availableString.replace(re, newSubStr);

 

alert(availableString);

alert(newAvailableString);

</script>

 

The replace method of the string object has an advantage over the test and exec methods of the regexp object, in that you do not have to call it over and over to get all of the possible matches with the global flag. You call the replace method once with the global flag, and all possible matches will be replaced.


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