Replacing and Spliting JavaScript Sub Strings - Splitting a String
(Page 4 of 4 )
Consider the following available string:
var availableString = "You need an exercise book or typing sheets of papers and a pen.";
We have two English language conjunctions in the string, which are "or" and "and." You can split the sentence into three sub-strings, based on the conjunctions. It is possible for you to have an array of the sub-strings that are separated by any of the conjunctions.
In the array the first element would be "You need an exercise book," the second element would be "typing sheets of papers" and the last element would be "a pen." So, there would be three elements in the array. The conjunctions would be omitted; they would not be in the array. The available string remains unchanged. You need a String split() function and a RegExp object for the conjunctions to achieve this. The following code illustrates this:
<html>
<head>
</head>
<body>
<script type = "text/javascript">
var availableString = "You need an exercise book or typing sheets of papers and a pen.";
var re = /or|and/;
var arrayOfStrs = availableString.split(re);
alert(arrayOfStrs);
</script>
</body>
</html>
The first statement declares the available string. The next statement forms the RegExp object. The pattern re would match either "or" or "and." You do not need a global flag. The split() method would scan the entire string. It takes the RegExp object as an argument. The split() method returns an array with the sub-strings in the order in which they are in the available string. If there is no sub-string to return, the split method returns the entire available string.
This is what the specification (JavaScript 1.5) has to say about the String split() method:
Syntax:
split([separator][, limit])
The split method returns the new array.
When found, separator is removed from the sub-strings and the sub-strings are returned in an array. If separator is omitted, the array contains one element consisting of the entire string. The original (available) string remains unchanged.
In JavaScript 1.2 or later, split has the following additions:
- It can take a limit count so that the resulting array does not include trailing empty elements.
- If you specify LANGUAGE="JavaScript1.2" in the SCRIPT tag, string.split(" ") splits on any run of 1 or more white space characters including spaces, tabs, line feeds, and carriage returns. For this behavior, LANGUAGE="JavaScript1.2" must be specified in the <SCRIPT> tag.
The limit Parameter
For our example above, if you want an array with just the first sub-string, then the argument for the limit parameter is 1. If you want an array with the first and second sub-strings (in their order), then the argument for the limit parameter is 2. If you want an array with the first, second and third sub-strings (in their order), then the argument for the limit parameter is 3.
We have come to the end of this two-part series. As you can see, whenever you have to choose between a RegExp object method and a String regular expression method, you should choose the String method, since the string methods are easier and more convenient to use.
| 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. |