Term
|
Definition
Matches any character, except for line breaks if dotall is false. |
|
|
Term
|
Definition
Matches 0 or more of the preceding character. |
|
|
Term
|
Definition
Matches 1 or more of the preceding character. |
|
|
Term
|
Definition
Preceding character is optional. Matches 0 or 1 occurrence. |
|
|
Term
|
Definition
|
|
Term
|
Definition
Matches any word character (alphanumeric & underscore). |
|
|
Term
|
Definition
Matches any single character from the character class. |
|
|
Term
|
Definition
Matches one or more of any of the characters in the set. |
|
|
Term
|
Definition
Matches the end of the string. |
|
|
Term
|
Definition
Matches the beginning of a string when not within a [character class]. |
|
|
Term
|
Definition
When inside of a character class, the ^ means NOT; in this case, match anything that is NOT a lowercase letter. |
|
|
Term
What's this do?
var username = 'JohnSmith'; alert(/[A-Za-z_-]+/.test(username)); |
|
Definition
begin by declaring a regular expression which only allows upper and lower case letters, an underscore, and a dash. We wrap these accepted characters within brackets, which designates a character class. The "+" symbol, which proceeds it, signifies that we're looking for one or more of any of the preceding characters. We then test that pattern against our variable, "JohnSmith." Because there was a match, the browser will display an alert box with the value, "true." |
|
|
Term
What's it do?
var str = 'this is my string'; alert(str.split(/\s/)); |
|
Definition
By passing "\s" - representing a single space - we've now split our string into an array. If you need to access one particular value, just append the desired index.
// alerts "this, is, my, string" |
|
|
Term
What's it do?
var str = 'this is my this string'; alert(str.split(/\s/)[3]); |
|
Definition
|
|
Term
var someString = 'Hello, World'; someString = someString.replace(/World/, 'Universe'); alert(someString); // alerts "Hello, Universe" |
|
Definition
|
|
Term
What's it do?
var username = 'J;ohnSmith;@%'; var username = 'J;ohnSmith;@%'; username = username.replace(/[^A-Za-z\d_-]+/g, ''); alert(username); |
|
Definition
|
|
Term
What's this do?
var string = 'This is just a string with some 12345 and some !@#$ mixed in.'; alert(string.match(/[a-z]+/gi)); |
|
Definition
alerts "This,is,just,a,string,with,some,and,some,mixed,in"
Within the regular expression, we created a pattern which matches one or more upper or lowercase letters - thanks to the "i" modifier. We also are appending the "g" to declare a global search. The code above will alert "This,is,just,a,string,with,some,and,some,mixed,in." If we then wanted to trap one of these values within the array inside of a variable, we just reference the correct index. |
|
|
Term
What's this do?
var string = 'This is just a string with some 12345 and some !@#$ mixed in.'; var matches = string.match(/[a-z]+/gi); alert(matches[2]); |
|
Definition
alerts "just"
Within the regular expression, we created a pattern which matches one or more upper or lowercase letters - thanks to the "i" modifier. We also are appending the "g" to declare a global search. The code above will alert "This,is,just,a,string,with,some,and,some,mixed,in." If we then wanted to trap one of these values within the array inside of a variable, we just reference the correct index. |
|
|
Term
What does each part of this do?
var email = 'nettuts@tutsplus.com'; alert(email.replace(/([a-z\d_-]+)@([a-z\d_-]+)\.[a-z]{2,4}/ig, '$1, $2')); |
|
Definition
.replace(/([a-z\d_-]+)
Starting from the middle, we search for any letter, number, underscore, or dash, and match one ore more of them (+). We'd like to access the value of whatever is matched here, so we wrap it within parentheses. That way, we can reference this matched set later!
@([a-z\d_-]+)
Immediately following the preceding match, find the @ symbol, and then another set of one or more letters, numbers, underscore, and dashes. Once again, we wrap that set within parentheses in order to access it later.
\.[a-z]{2,4}/ig,
Continuing on, we find a single period (we must escape it with "\" due to the fact that, in regular expressions, it matches any character (sometimes excluding a line break). The last part is to find the ".com." We know that the majority, if not all, domains will have a suffix range of two - four characters (com, edu, net, name, etc.). If we're aware of that specific range, we can forego using a more generic symbol like * or +, and instead wrap the two numbers within curly braces, representing the minimum and maximum, respectively.
'$1, $2')
This last part represents the second parameter of the replace method, or what we'd like to replace the matched sets with. Here, we're using $1 and $2 to refer to what was stored within the first and second sets of parentheses, respectively. In this particular instances, $1 refers to "nettuts," and $2 refers to "tutsplus." |
|
|