Java Reference
In-Depth Information
myString = myString + “Hello World</td></tr></table>”;
myString = myString +”<br><h2>Heading</h2>”;
Next you create your RegExp object and initialize it to your regular expression.
var myRegExp = /<[^>\r\n]+>|[^<>\r\n]+/g;
Let's break it down to see what pattern you're trying to match. First, note that the pattern is broken up
by an alternation symbol: |. This means that you want the pattern on the left or the right of this symbol.
You'll look at these patterns separately. On the left, you have the following:
The pattern must start with a
< .
In
[^>\r\n]+, you specify that you want one or more of any character except the > or a \r
(carriage return) or a \n (linefeed).
specifi es that the pattern must end with a > .
>
On the right, you have only the following:
[^<>\r\n]+
specifi es that the pattern is one or more of any character, so long as that character
is not a <, >, \r, or \n. This will match plain text.
After the regular expression defi nition you have a g, which specifi es that this is a global match.
So the <[^>\r\n]+> regular expression will match any start or close tags, such as <p> or </p> . The
alternative pattern is [^<>\r\n]+ , which will match any character pattern that is not an opening or
closing tag.
In the following line, you assign the resultsArray variable to the Array object returned by the
match() method:
var resultsArray = myString.match(myRegExp);
The remainder of the code deals with populating the text area with the split HTML. You use the Array
object's join() method to join all the array's elements into one string with each element separated by a
\r\n character, so that each tag or piece of text goes on a separate line, as shown in the following:
document.form1.textarea1.value = “”;
document.form1.textarea1.value = resultsArray.join(“\r\n”);
}
Using the RegExp Object's Constructor
So far you've been creating RegExp objects using the / and / characters to defi ne the start and end of the
regular expression, as shown in the following example:
var myRegExp = /[a-z]/;
Search WWH ::




Custom Search