Episodes
Monday Nov 13, 2006
Dealing browser cache in ajax
Monday Nov 13, 2006
Monday Nov 13, 2006
All browsers maintain a so-called cache of visited web pages, a local record of page contents stored on the hard disk of the browser's computer. When you request a particular web page, the browser first tries to load the page from its cache, rather than submitting a new HTTP request. A commonly used trick to work around this problem involves the adding of a parameter with a random and meaningless value to the request data. In the case of a GET request, this necessitates adding a further parameter and value pair to the end of the URL. If the random part of the URL is different each time, this effectively "fools" the browser into believing that it is to send the asynchronous request to an address not previously visited. This results in a new HTTP request being sent on every occasion. In javascript, you can use the Math.random() method of the native Math() object to create the random data.
Saturday Nov 11, 2006
RegExp classes
Saturday Nov 11, 2006
Saturday Nov 11, 2006
Character Classes
Character classes are groups of characters to test for. By enclosing characters inside of square brackets, you are effectively telling the regular expression to match the first character, the second character, the third character, or so on. For example, to match the characters a, b, and c, the character class is [abc]. This is called a simple class, because it specifies the exact characters to look for.
Simple Classes
Suppose you want to match "bat", "cat", and "fat". It is very easy to use a simple character class for this purpose:
var strCheck = "a bat, a Cat, a fAt baT, a faT cat";
var reBatCatRat = /[bcf]at/gi;
var arrMatches = strCheck.match(reBatCatRat);
Negation Classes
At times you may want to match all characters except for a select few. In this case, you can use a negation class, which specifies characters to exclude. For example, to match all characters except a and b, the character class is [^ab]. The caret (^) tells the regular expression that the character must not match the characters to follow.
Going back to the previous example, what if you only wanted to get words containing at but not beginning with b or c?
var strCheck = "a bat, a Cat, a fAt baT, a faT cat"; var reBatCatRat = /[^bc]at/gi; var arrMatches = strCheck.match(reBatCatRat);
In this case, arrMatches contains "fAt" and "faT", because these strings match the pattern of a sequence ending with at but not beginning with b or c.
Range Classes
Up until this point, the character classes required you to type all the characters to include or exclude. Suppose that you want to match any alphabet character, but you really don't want to type every letter in the alphabet. Instead, you can use a range class to specify a range between a and z: [a-z]. The key here is the dash (-), which should be read as through instead of minus (so the class is read as a through z not a minus z).
Important | Note that [a-z] matches only lowercase letters unless the regular expression is set to case insensitive by using the i option. To match only uppercase letters, you must use [A-Z]. |
Range classes work whenever the characters you want to test are in order by character code. Consider the following example:
var strCheck = "num1, num2, num3, num4, num5, num6, num7, num8, num9"; var reOneToFour = /num[1-4]/gi; var arrMatches = strCheck.match(reOneToFour);
After execution, arrMatches contains four items: "num1", "num2", "num3", and "num4" because they all match num and are followed by a character in the range 1 through 4.
Important | You can also negate range classes so as to exclude all characters within a given range. For example, to exclude characters 1 through 4, the class is [^1-4]. |
Combination Classes
A combination class is a character class that is made up of several other character classes. For instance, suppose you want to match all letters a through m, numbers 1 through 4, and the new line character. The class looks like this:
[a-m1-4\n]
Note that there are no spaces between the different internal classes.
Important | JavaScript/ECMAScript doesn't support union and intersection classes as do other regular expression implementations. This means you can't make patterns such as [a-m[p-z]] or [a-m[^b-e]]. |
Saturday Nov 11, 2006
RegExp Boundaries
Saturday Nov 11, 2006
Saturday Nov 11, 2006
Boundaries are used in regular expressions to indicate the location of a pattern. The following table lists the available boundaries:
Boundary |
Description |
---|---|
^ |
Beginning of the line |
$ |
End of the line |
|
Word boundary |
B |
Non-word boundary |
Saturday Nov 11, 2006
RegExp Quantifiers
Saturday Nov 11, 2006
Saturday Nov 11, 2006
Quantifiers enable you to specify how many times a particular pattern should occur.
The following table lists the various ways to quantify a particular pattern.
Code |
Description |
---|---|
? |
Either zero or one occurrence |
* |
Zero or more occurrences |
+ |
One or more occurrences |
{n} |
Exactly n occurrences |
{n,m} |
At least n but no more than m occurrences |
{n,} |
At least n occurrences |
Saturday Nov 11, 2006
Javascript RegExp Predifined classes
Saturday Nov 11, 2006
Saturday Nov 11, 2006
Predefined Classes
Javascript has a set of predefined character classes for you to specify some complex classes. The following table lists all the predefined classes:
Code |
Equal To |
Matches |
---|---|---|
. |
[^ ] |
Any character except new line and carriage return |
d |
[0-9] |
A digit |
D |
[^0-9] |
A non-digit |
s |
[ x0Bf ] |
A white-space character |
S |
[^ x0Bf ] |
A non-white–space character |
w |
[a-zA-Z_0-9] |
A word character (all letters, all numbers, and an underscore) |
W |
[^a-zA-Z_0-9] |
A non-word character |
Suppose you want to match three numbers, without using d. Your code looks like this:
var sToMatch = "567 9838 abc";var reThreeNums = /[0-9][0-9][0-9]/;
alert(reThreeNums.test(sToMatch)); //outputs "true"
Using d, the regular expression becomes much cleaner:
var sToMatch = "567 9838 abc";var reThreeNums = /ddd/;
alert(reThreeNums.test(sToMatch)); //outputs "true"
Saturday Nov 11, 2006
Javascript RegExp -1
Saturday Nov 11, 2006
Saturday Nov 11, 2006
In javascript, the RegExp object has a method called test(), which simply returns true if the given string (the only argument) matches the pattern and false if not:
var sMyString = "get a dog"; var regPattern = /dog/; alert(regPattern.test(sMyString)); //outputs "true"The RegExp exec() method, which takes a string as an argument, returns an Array containing all matches. Consider the following example:
var sToMatch = "a bat, a Cat, a fAt baT, a faT cat";
var reAt = /at/g;
var arrMatches = reAt.exec(sToMatch);
The replace() method, which replaces all occurrences of a substring (the first argument) with a different string (the second argument). For example:
var sToChange = "The sky is red. "; alert(sToChange.replace("red", "blue")); //outputs "The sky is blue."
Saturday Nov 11, 2006
Javascript Regular Expression
Saturday Nov 11, 2006
Saturday Nov 11, 2006
JavaScript supports regular expressions. The constructor for a RegExp object takes one or two arguments. The first (or only) argument is the string describing the pattern to match; if there is a second argument, it is a string specifying additional processing instructions.
The most basic regular expression is a regular string. For instance, to match the word "cat", you can define the regular expression like this:
var reCat = new RegExp("cat");
This regular expression matches only the first occurrence of the word "cat" in a string, and it is case-sensitive. To make the regular expression match all occurrences of "cat", you can add the second argument to the constructor:
var reCat = new RegExp("cat", "g");
In this line, the second argument "g" is short for global, meaning that the entire string is searching for occurrences of "cat" instead of stopping after the first occurrence. If you want to make the pattern case-insensitive, you can add the character "i" to the second argument ("i" is short for insensitive, as in case-insensitive):
var reCat = new RegExp("cat", "gi");
Some regular expression literals use Perl-style syntax:
var reCat = /cat/gi;
Regular expression literals begin with a forward slash, followed by the string pattern, followed by another forward slash. If you want to specify additional processing instructions, such as "g" and "i", these come after the second forward slash (as in the previous example).