Try the regular expression practice program.
Regular expression symbols as documented by developer.mozilla.org the developer of Firefox. Gives an explanation and examples for all the regular expression symbols. Here is the subset of symbols to know and be familiar with.
Here are a couple of interesting web sites discussing common regular expressions. Regular-expression.info and 8 Regular Expressions You Should Know - Code Tuts
To start you have an alphabet of characters.
For example the characters that you can type on a keyboard.
Putting a sequence of characters together you have a string.
For example "This is a string".
You can then have a set of strings; for example {"ax", "bx" "cx", "dx"}.
A regular expression describes a set of strings over an
alphabet of characters.
For example {"ax", "bx" "cx", "dx"} can be described by the
regular expression [abcd]x
While {"xa", "xb" "xc", "xd"} can be described by the
regular expression x[abcd]
Regular expressions may also include the following search modifiers.
Look at examples in developer.mozilla.org webpage on regular expressions.
Try the examples in a script followed by console.log(Result) statements.
In JavaScript a literal regular expression is denoted using the "/" character.
var theRegularExpression = /db+d/g;
var theString = "cdbbdbsbz";
var Result = theString.replace(theRegularExpression, "123");
An alternative is to use the regular expression constructor.
var theRegularExpression = new RegExp("db+d","g");
var theString = "cdbbdbsbz";
var Result = theString.replace(theRegularExpression, "123");
Use regular expressions to swap names.
var theRegularExpression = /(\w+) (\w+)/;
var theString = "Leila Anderson";
var Result = theString.replace(theRegularExpression, "$2, $1");
Previous example with '\' as the separator. Need to escape the '\' in the regular expression and in the string.
var theRegularExpression = /(\w+)\\(\w+)/;
var theString = "Leila\\Anderson";
var Result = theString.replace(theRegularExpression, "$2, $1");
An alternative is to use the regular expression constructor. In JavaScript you
have to use '\\' in the string to get a single '\' in the pattern.
var theRegularExpression = new RegExp("(\\w+) (\\w+)","");
var theString = "Leila Anderson";
var Result = theString.replace(theRegularExpression, "$2, $1");
Previous example with '\' as the separator. Now need four '\' in the
regular expression string to leave two '\' for the regular expression
interpreter.
var theRegularExpression = new RegExp("(\\w+)\\\\(\\w+)","");
var theString = "Leila\\Anderson";
var Result = theString.replace(theRegularExpression, "$2, $1");
The function escapeRegExp(string) – puts the escape character in front of
every character that needs to be escaped in string to prevent it from being
a special character in a regular expression
function escapeRegExp(string){
return string.replace(/([.*+?^${}()|[\]\\])/g, "\\$&");
}
// Use the function
var theString = "[ ] { } ( ) . + * $ ? \\ |";
// String in which to escape characters
if (escapeRegExp("[ ] { } ( ) . + * $ ? \\ |") === // Compare result
"\\[ \\] \\{ \\} \\( \\) \\. \\+ \\* \\$ \\? \\\\ \\|") {
console.log("Escaped");
}
else { console.log("Still a prisoner"); }
console.log(theString.length + " " + escapeRegExp(theString).length);
console.log(">>>" + theString + "<<<");
console.log(">>>" + escapeRegExp(theString) + "<<<");
Try the examples in a script followed by console.log() statements for the variables.
Given a string of name-pairs, firstName lastName, with each pair separated by a semi-colon, and with arbitrary white space create a string of names with the first and last names swapped.
var names = "Harry Trump ; Fred Barney; Helen Rigby ; Bill Abel ; Chris Hand ";
var namePattern = /(\w+)\s+(\w+)/g; // firstName whiteSpace lastName
var newNames = names.replace(namePattern, "$2, $1");
Given a string of name-pairs, firstName lastName, with each pair separated by a semi-colon, and with arbitrary white space create a list of names with the first and last names swapped.
var names = "Harry Trump ; Fred Barney; Helen Rigby ; Bill Abel ; Chris Hand ";
var splitPattern = /\s*;\s*/; // All white space before and after a ';'
var nameList = names.split(splitPattern);
var namePattern = /(\w+)\s+(\w+)/; // firstName whiteSpace lastName
var namesSwappedList = []; // Create an empty list
var index = 0;
while (index < nameList.length) {
namesSwappedList[index] = nameList[index].replace(namePattern, "$2, $1");
index += 1
}
An alternate way of doing the previous problem.
var names = "Harry Trump ; Fred Barney; Helen Rigby ; Bill Abel ; Chris Hand ";
var namePattern = /(\w+)\s+(\w+)/g; // firstName whiteSpace lastName
var newNames = names.replace(namePattern, "$2, $1");
var splitPattern = /\s*;\s*/; // All white space before and after a ';'
var namesSwappedList = newNames.split(splitPattern);
A phone number consists of a three-digit area code followed by a three-digit
exchange followed by a four-digit local number.
Variations are to permit
the area code to be with parenthesis, e.g. (905), and to permit any of the
characters ' '(space), '.' or '-' to separate the three parts, e.g.
123 456 9876, but the same character must be used in both places.
it is important to document complex regular expressions.
Look at the JavaScript in the regular expression practice program.