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.
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.