Chapter 5
Regular Expression, Rollover and Frames
What is Regular Expression?
Syntax:
or simply
Where
Reg. Exp. Cont. :
Ex:
/kkwp/i is a regular expression.
Language of Reg. Exp.:
Language of Reg. Exp. Cont.:
Language of Reg. Exp. Cont.:
1. The ‘/’ character tells the browser that this is the beginning of a regular expression.
2. The ‘[‘ character tells the browser to search the text for the following character(s).
3. The ‘\’ tells the browser to ignore the special meaning of the next character.
4. The ‘[‘ character is the character that the browser will search for in the text.
5. The ‘]’ character tells the browser that there are no more characters to search for.
6. The ‘/’ character tells the browser that this is the end of the regular expression.
Quantifiers:
Quantifiers Cont.:
Sr. No. | Expression | Description |
1. | p+ | It matches any string containing one or more p's. |
2. | p* | It matches any string containing zero or more p's. |
3. | p? | It matches any string containing at most one p. |
4. | p{N} | It matches any string containing a sequence of N p's |
5. | p{2,3} | It matches any string containing a sequence of two or three p's. |
6. | p{2, } | It matches any string containing a sequence of at least two p's. |
7. | p$ | It matches any string with p at the end of it. |
8. | ^p | It matches any string with p at the beginning of it. |
Example:
Sr. No. | Expression | Description |
1. | [^a-zA-Z] | It matches any string not containing any of the characters ranging from a through z and A through Z. |
2. | p.p | It matches any string containing p, followed by any character, in turn followed by another p. |
3. | ^.{2}$ | It matches any string containing exactly two characters. |
4. | <b>(.*)</b> | It matches any string enclosed within <b> and </b>. |
5. | p(hp)* | It matches any string containing a p followed by zero or more instances of the sequence hp. |
Literal Characters:
Sr. No. | Character | Description |
1. | Alphanumeric | Itself |
2. | \0 | The NUL character (\u0000) |
3. | \t | Tab (\u0009) |
4. | \n | Newline (\u000A) |
5. | \v | Vertical tab (\u000B) |
6. | \f | Form feed (\u000C) |
7. | \r | Carriage return (\u000D) |
8. | \xnn | The Latin character specified by the hexadecimal number nn; for example, \x0A is the same as \n |
9. | \uxxxx | The Unicode character specified by the hexadecimal number xxxx; for example, \u0009 is the same as \t |
10. | \cX | The control character ^X; for example, \cJ is equivalent to the newline character \n |
Metacharacters:
Explanation:
Sr. No. | Character | Description |
1. | . | a single character |
2. | \s | a whitespace character (space, tab, newline) |
3. | \S | non-whitespace character |
4. | \d | a digit (0-9) |
5. | \D | a non-digit |
6. | \w | a word character (a-z, A-Z, 0-9, _) |
7. | \W | a non-word character |
8. | [\b] | a literal backspace (special case). |
9. | [aeiou] | matches a single character in the given set |
10. | [^aeiou] | matches a single character outside the given set |
11. | (foo|bar|baz) | matches any of the alternatives specified |
/[ ^ \- ]/
Finding Nonmatching Characters:
Entering a Range of Characters:
Sr. No. | Expression | Description |
1. | [...] | Any one character between the brackets. |
2. | [^...] | Any one character not between the brackets. |
3. | [0-9] | It matches any decimal digit from 0 through 9. |
4. | [a-z] | It matches any character from lowercase a through lowercase z. |
5. | [A-Z] | It matches any character from uppercase A through uppercase Z. |
The ranges shown above are general; you could also use the range [0-3] to match any decimal digit ranging from 0 through 3, or the range [b-v] to match any lowercase character ranging from b through v
Matching Digits and Nondigits:
Matching Punctuation and Symbols:
Matching Words:
Matching Words Cont.:
The test() method:
<script>
function RegExpression() {
var name='Bob'
re = /[bt]/
if (re.test(name))
{ alert('Found') }
else
{ alert('Not Found') }
}
</script>
Output:
The test() method Cont.:
Replace Text Using a Regular Expression:
re = / \b Bob \b /
text = 'Hello, Bob and welcome to our web site.'
text = text.replace(re, 'Mary')
Placing ‘g’ at the end of the regular expression, which tells the browser to replace all occurrences of the regular expression in the string.
Replace Text Using a Regular Expression:
Replacing Like Values:
Replacing Like Values Cont.:
Return the Matched Characters:
Return the Matched Characters Cont.:
/ \b Bob.* \b /
We'll need to do the following:
1. Create the regular expression object and assign it the regular expression: re = / \b Bob.* \b /
2. Call the exec() method, passing it the text and assigning the return value to an array variable. re = / \b Bob.* \b /
MyArray = re.exec('Hello, my name is Bobby.')
3. We then display the value of the first array element:
re = / \b Bob.* \b /
MyArray = re.exec('Hello, my name is Bobby.')
alert('Welcome, ' + MyArray[0]).
Symbols to remember for Regular Expression:
Reg.Exp. Properties:
Sr. No. | Reg. Exp. Object | Properties |
1. | $1 (through $9) | Parenthesized substring matches |
2. | $_ | Same as input |
3. | $* | Same as multiline |
4. | $& | Same as last Match |
5. | $+ | Same as last Parenthesis |
6. | $` | Same as left Context |
7. | $' | Same as right Context |
8. | constructor | Specifies the function that creates an object's prototype |
9. | global | Search globally (g modifier in use) |
10. | ignoreCase | Search case-insensitive (i modifier in use) |
11. | input | The string to search if no string is passed |
Reg.Exp. Properties Cont.:
Sr. No. | Reg. Exp. Object | Properties |
12. | lastIndex | The index at which to start the next match |
13. | lastMatch | The last match characters |
14. | lastParen | The last parenthesized substring match |
15. | leftContext | The substring to the left of the most recent match |
16. | multiline | Whether strings are searched across multiple lines |
17. | prototype | Allows the addition of properties to all objects |
18. | rightContext | The substring to the right of the most recent match |
19. | source | The regular expression pattern itself |
Syntax
RegExp.constructor
Return Value
Returns the function that created this object's instance.
Reg.Exp. Properties:
Example:
<html>
<head>
<title>JavaScript RegExp constructor Property</title>
</head>
<body>
<script type = "text/javascript">
var re = new RegExp( "string" );
document.write("re.constructor is:" + re.constructor);
</script>
</body>
</html>
2. global: global is a read-only boolean property of
RegExp objects. It specifies whether a particular
regular expression performs global matching, i.e., whether it was created with the "g" attribute.
Syntax
RegExpObject.global
Return Value
Returns "TRUE" if the "g" modifier is set, "FALSE" otherwise.
RegExp Properties:
Example:
<html>
<head></head>
<body>
<script type = "text/javascript">
var re = new RegExp( "string" );
if ( re.global ) document.write("Test1 - Global property is set");
else document.write("Test1 - Global property is not set");
re = new RegExp( "string", "g" );
if ( re.global ) document.write("<br />Test2 - Global property is set");
else document.write("<br />Test2 - Global property is not set");
</script>
</body>
</html>
3. ignoreCase: ignoreCase is a read-only boolean property of RegExp objects. It specifies whether a particular regular expression performs case- insensitive matching, i.e., whether it was created with the "i" attribute.
Syntax
RegExpObject.ignoreCase
Return Value
Returns "TRUE" if the "i" modifier is set, "FALSE" otherwise.
RegExp Properties:
Example:
<html>
<head></head>
<body>
<script type = "text/javascript">
var re = new RegExp( "string" );
if ( re.global ) document.write("Test1 - Global property is set");
else document.write("Test1 - Global property is not set");
re = new RegExp( "string", "g" );
if ( re.global ) document.write("<br />Test2 - Global property is set");
else document.write("<br />Test2 - Global property is not set");
</script>
</body>
</html>
4. lastIndex: It is a read/write property of RegExp objects.
Syntax: RegExpObject.lastIndex
Return Value
Returns an integer that specifies the character position immediately following the last match.
RegExp Properties:
Example:
<html>
<head></head>
<body>
<script type = "text/javascript">
var str = "Javascript is an interesting scripting language";
var re = new RegExp( "script", "g" );
re.test(str);
document.write("Test 1 - Current Index: " + re.lastIndex);
re.test(str);
document.write("<br />Test 2 - Current Index: " + re.lastIndex);
</script>
</body>
</html>
Frames:
Frames:
Frames:
<frameset>:
<frame> tag:
<frame> tag Cont.:
Ex:
<!—Dividing a Screen using <franeset>
<html>
<head> </head>
<frameset rows="50% , 50%">
<frame src="WebPage1.html" name="topPage" />
<frame src="WebPage2.html" name="bottomPage" />
</frameset>
</html>
<frame> tag Cont.:
Ex:
<html>
<head></head>
<frameset rows="50%,50%">
<frame src="WebPage1.html" name="topPage"
frameborder="0" border="0" />
<frame src="WebPage2.html" name="bottomPage"
frameborder="0" border="0" />
</frameset> </html>
Invisible Borders:
Calling a Child Window's JavaScript Function:
Changing the Content of a Child Window:
Changing the Focus of a Child Window:
Writing to a Child Window:
Accessing Elements of Another Child Window:
Rollover:
Ex: <IMG height="92" src="7441805.gif" width="70"
border="0" onmouseover="src='0072253630.jpeg'">
Creating Rollover:
Creating Rollback:
Multiple Actions for a Rollover:
More Efficient Rollovers: