String ObjectJavaScript includes a String object that allow us to manipulate strings in a variety of ways, for example, searching a string to see if it contains certain patterns of letters, extracting part of it to form new string, and much more.
A String object is created in the following way: |
|
var myString = new String("Hello World"); |
|
However, most browsers regard any string as
an instance of the String object. Therefore you
can declare a string in the normal way, e.g.: |
|
var myString = "Hello World"; |
|
...and in so doing you will automatically
create a String object, complete with its associated
methods, properties, etc.. |
|
String properties include:
The examples given above indicate only a few of the possibilities offered by regular expressions.
Some of the most commonly-used pattern-matching characters are shown below. For a more complete list you should consult a good reference book (the 'Pure JavaScript' book recommended for use with this course has quite an extensive list).
| \w | Represents any alphanumerical character |
| \W | Represents any non-alphanumerical character |
| \d | Represents any numerical character |
| \D | Represents any non-numerical character |
| \s | Represents any 'whitespace' character (e.g., carriage-return, tab, etc.) |
| \S | Represents any non-whitespace character |
| [..] | Match any one of the characters within the brackets |
| [^..] | Match any one character other than those within the brackets |
| [x-y] | Match any character within the range x to y |
| [^x-y] | Match any one character other than those within the range x to y |
| {x} | Match the previous item x times |
| {x,} | Match the previous item at least x times |
In addition to the methods described above, the String object has a number of methods that are specifically designed to work with regular expressions.