Lesson 07
 
Lesson 7 - Reading

REQUIRED:

  • Read and do the practice work in Chapter 9
Lesson 7 - Notes and Practice Work

Working with Regular Expressions

Regular Expression special characters

\

Toggles between literal and special characters

^ Beginning of a string
$

End of a string

* Zero or more times
+ One or more times
? Zero or one time
. Any character except newline
\b Word boundary
\B Non-word boundary
\d Any digit 0 - 9
\D Any non-digit
\f Form feed
\n New line
\r Carriage return
\s Any single white space
\S Any sing non-white space character
\t Tab
\v Vertical Tab
\w

Any Letter, number or the underscore

\W Any character other than a letter, number or underscore
\xnn hexadecimal number
\onn octal number
\cX control character X
[abcde] Character set that matches one of the enclosed characters
[^abcde] Character set that does not match one of the enclosed characters
[a-e] A character set that matches any one in the range of enclosed characters
[\b] Backspace character
{n}
Exactly n occurrences of the previous character
{n,} At least n occurrences of the previous character
{n,m} Between n and m occurrences of the previous character
() A grouping, which is also stored
x|y Either X or Y
 
Modifiers
g Search for all possible mathces
i Search without case-sensitivity

Validating an email address with regular expressions

<script>

re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/

function submitIt(form) {
if (re.test(form.emailAddr.value)) {
return true
}
alert("Invalid email address")
form.emailAddr.focus()
form.emailAddr.select()
return false
}

</script>

<body>

<form onsubmit="return submitIt(this)" action="somepage.cgi">
<p>email address:
<input type="text" name="emailAddr">
<input type="submit" value="submit">
</p>

</form>
</body>

Validating an image file with regular expressions

<script>

re = /^(file|http):\/\/\S+\/\S+\.(gif|jpg)$/i

function submitIt(form) {

if (re.test(form.imgURL.value)) {
document.chgImg.src = form.imgURL.value
}

else {
alert("Invalid URL ")
form.imgURL.focus()
form.imgURL.select()
}

return false

}

</script>

<body>

<form onsubmit="return submitIt(this)" action="somepage.cgi">
<p>Image URL :
<input type="text" name="imgURL" size="50">
<input type="submit" value="submit">
</p>

<p>
<img src="" name="chgImg"
</p>

</form>
</body>

 

Formatting and Validating Strings

<script>

re = /^\(?(\d{3})\)?[\.\-\/ ]?(\d{3})[\.\-\/ ]?(\d{4})$/

function submitIt(form) {

validPhone = re.exec(form.phone.value)
if (validPhone) {
form.phone.value = "(" + validPhone[1] + ") " + validPhone[2] + "-" + validPhone[3]
}
else {
alert(form.phone.value + " isn't a vlaid phone number")
form.phone.focus()
form.phone.select()
}
return false
}

</script>

<body>
<form onsubmit="return submitIt(this)" >

<p>Phone Number (with area code): <input name="phone" type="text"> <input type="submit" value="submit">
</p>
</form>
</body>

 


 

Lesson 7 - Assignment

  1. Use the example scripts and form to recreate the following:

    email address:

    Phone Number (with area code):

    Image URL :

    (try ... http://www.gc.maricopa.edu/business/cis166aa/images/cis166aa.jpg )


  2. Save the file as CIS166AA07.HTM
  3. Upload the file and any images used to your CIS166aa folder on Gecko.

Send in Your Assignment to be Graded

After you have read these instructions, click on the link "Submit my assignment for grading" below. This will create a message for you to use to send in your assignment to the CIS166AA grader. It already will have the message address and the subject line filled in. DO NOT CHANGE THE ADDRESS OR THE SUBJECT LINE. Enter the following required information as the message text:

  • your full name
  • your palette account username
    • If you do not know your palette username, use your student ID card at a palette account lookup station in HTC1 or HTC2. If you are off campus, call the Student Help Desk at 623.845.HELP (4357) or send an email message to student-helpdesk@student.gc.maricopa.edu to request your palette username.
  • you email address
  • course section number
  • Include the full URL to lesson 7
    ( ie. http://gecko.gc.maricopa.edu/~palette_username/cis166aa/cis166aa07.htm )

Only by following this process can you be guaranteed that your assignment will get graded and the score recorded correctly.

Submit my assignment for grading

 


  Back to Top