uk
Regular Expressions
Web Pages: Behaviour
CSC1014
Regular Expressions
A "standardised" pattern matching syntax for text
Define pattern test against input ...and second... and third...
something(.something)@something.xx(or.xxx)(.xx or .xxx)
JISC Netskills
CSC1014
CSC1014
Any character A digit A non-digit A whitespace character A non-whitespace character A word character A non-word character
CSC1014
JISC Netskills
CSC1014
Try matching the opening <b> tag in... A simple pattern should work...
/<.+>/
But the quantifier + is greedy and keeps matching until it reaches the end of the string to find...
<b>bold</b> text
Backtrack to find the end of the pattern i.e. the last >
CSC1014
Now it backtracks after each match to complete the pattern...meaning a match occurs after the first > character
<b> <b>bold</b> text Backtrack each time to find the end of the pattern i.e. the first >
http://www.regular-expressions.info/repeat.html
JISC Netskills
CSC1014
Matches Use case-insensitive matching Global matching (instead of stopping at first match) Multiline mode
CSC1014
The RegExp object is defined as a pattern to match RegExp object methods use/test/apply pattern where needed
var pattern = /^[a-z]+$/i; if (pattern.test(someString)){ alert("Yay") } else { alert("Boo"); } Tests string against the expression
JISC Netskills
CSC1014
thisPattern.exec(someString); Return an array of info about the thisPattern.test(someString); Return true or false if string
JISC Netskills
CSC1014
Purpose Return position of first substring match (-1 if no match) Replace the text matched by expression with string in second parameter Return and array containing all the matches for the expression
JISC Netskills
CSC1014
if (thisForm.inputbox.value != ""){ var pattern = /^[a-z]+$/i; if (pattern.test(thisForm.inputbox.value)){ //SUCCESS :-) } else { //FAILURE :-( } }
JISC Netskills
Act on outcome
CSC1014
JavaScript-based
http://tools.netshiftmedia.com/regexlibrary http://www.regular-expressions.info/javascriptexample.html
http://www.spaweditor.com/scripts/regex/
CSC1014