Home arrow JavaScript arrow Page 3 - JavaScript Objects: Finishing Strings
JAVASCRIPT

JavaScript Objects: Finishing Strings


In the last tutorial we continued our discussion on String Objects in JavaScript. Hopefully we will wrap it up in this episode. We left off with the Match() method, which allowed us to search a string and return the same string if it was present in our variable, or return a null value if it was not. We will pick back up with the Replace() method.

Author Info:
By: James Payne
Rating: 5 stars5 stars5 stars5 stars5 stars / 3
January 28, 2008
TABLE OF CONTENTS:
  1. · JavaScript Objects: Finishing Strings
  2. · The Replace() Method
  3. · The Search() Method
  4. · The Slice() Method
  5. · The Strike(), Sub(), Small() and Sup() Methods
  6. · The ToLowerCase() and ToUpperCase Methods

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
JavaScript Objects: Finishing Strings - The Search() Method
(Page 3 of 6 )

There are a lot of ways to find out if a string holds a particular value, as you have already seen. Why not add another? The Search() method will find your value and return its position in the string, or -1 if it fails to find it.


<html>

<body>


<script type="text/javascript">


var who="Go Go Gorillas!"


document.write(who.search("go") + "<br />")

document.write(who.search("Go") + "<br />")

document.write(who.search("Gorilla") + "<br />")


</script>


</body>

</html>

This results in:

  -1
  0
  6

Since the Search() method is case-sensitive, it returned a -1 when searching for the word "go." To remedy this situation, we simply add an “i” as in our previous programs and replace the quotation marks with forward slashes(/): 


<html>

<body>


<script type="text/javascript">


var who="Go Go Gorillas!"


document.write(who.search(/go/i) + "<br />")

document.write(who.search("Go") + "<br />")

document.write(who.search("Gorilla") + "<br />")


</script>


</body>

</html>

This results in:

  0
  0
  6
 


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