RegExp. Regular Expression A regular expression is a certain way to describe a pattern of characters. Pattern-matching or keyword search. Regular expressions.

Slides:



Advertisements
Similar presentations
Chapter 14 Perl-Compatible Regular Expressions Part 1.
Advertisements

Regular Expression Original Notes by Song Guo. What Regular Expressions Are Exactly - Terminology a regular expression is a pattern describing a certain.
Asp.NET Core Vaidation Controls. Slide 2 ASP.NET Validation Controls (Introduction) The ASP.NET validation controls can be used to validate data on the.
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
W3101: Programming Languages (Perl) 1 Perl Regular Expressions Syntax for purpose of slides –Regular expression = /pattern/ –Broader syntax: if (/pattern/)
JavaScript, Fourth Edition
JavaScript, Third Edition
Regular Expressions & Automata Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Introduction to scripting
Regex Wildcards on steroids. Regular Expressions You’ve likely used the wildcard in windows search or coding (*), regular expressions take this to the.
Regular Expressions in ColdFusion Applications Dave Fauth DOMAIN technologies Knowledge Engineering : Systems Integration : Web.
REGULAR EXPRESSIONS CHAPTER 14. REGULAR EXPRESSIONS A coded pattern used to search for matching patterns in text strings Commonly used for data validation.
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
XP Tutorial 14 New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Last Updated March 2006 Slide 1 Regular Expressions.
Regular Expressions Week 07 TCNJ Web 2 Jean Chu. Regular Expressions Regular Expressions are a powerful way to validate and format text strings that may.
Tutorial 14 Working with Forms and Regular Expressions.
Language Recognizer Connecting Type 3 languages and Finite State Automata Copyright © – Curt Hill.
Regular Expression Darby Tien-Hao Chang (a.k.a. dirty) Department of Electrical Engineering, National Cheng Kung University.
Pattern matching with regular expressions A common file processing requirement is to match strings within the file to a standard form, e.g. address.
 Text Manipulation and Data Collection. General Programming Practice Find a string within a text Find a string ‘man’ from a ‘A successful man’
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Regular Expression (continue) and Cookies. Quick Review What letter values would be included for the following variable, which will be used for validation.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
CS346 Javascript -3 Module 3 JavaScript Variables.
REGEX. Problems Have big text file, want to extract data – Phone numbers (503)
Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming regular expressions.
JavaScript, Part 2 Instructor: Charles Moen CSCI/CINF 4230.
12. Regular Expressions. 2 Motto: I don't play accurately-any one can play accurately- but I play with wonderful expression. As far as the piano is concerned,
CS346 Regular Expressions1 Pattern Matching Regular Expression.
GREP. Whats Grep? Grep is a popular unix program that supports a special programming language for doing regular expressions The grammar in use for software.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Copyright © Curt Hill Regular Expressions Providing a Search Pattern.
JavaScript III ECT 270 Robin Burke. Outline Validation examples password more complex Form validation Regular expressions.
1 Validating user input is the bane of every software developer’s existence. When you are developing cross-browser web applications (IE4+ and NS4+) this.
Java Script Pattern Matching Using Regular Expressions.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Ajmer Singh PGT(IP) Programming Fundamentals. Ajmer Singh PGT(IP) Java Character Set Character set is a set of valid characters that a language can recognize.
Validation using Regular Expressions. Regular Expression Instead of asking if user input has some particular value, sometimes you want to know if it follows.
Unit 11 –Reglar Expressions Instructor: Brent Presley.
Part:2.  Keywords are words with special meaning in JavaScript  Keyword var ◦ Used to declare the names of variables ◦ A variable is a location in the.
INT222 – Internet Fundamentals Week 11: RegExp Object and HTML5 Form Validation 1.
XP Tutorial 7 New Perspectives on JavaScript, Comprehensive 1 Working with Forms and Regular Expressions Validating a Web Form with JavaScript.
Basic Scripting & Variables Yasar Hussain Malik - NISTE.
-Joseph Beberman *Some slides are inspired by a PowerPoint presentation used by professor Seikyung Jung, which was derived from Charlie Wiseman.
JavaScript Syntax Fort Collins, CO Copyright © XTR Systems, LLC Introduction to JavaScript Syntax Instructor: Joseph DiVerdi, Ph.D., MBA.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Lesson 4 String Manipulation. Lesson 4 In many applications you will need to do some kind of manipulation or parsing of strings, whether you are Attempting.
Regular Expressions In Javascript cosc What Do They Do? Does pattern matching on text We use the term “string” to indicate the text that the regular.
Chapter 6 JavaScript: Introduction to Scripting
Strings and Serialization
Lecture 19 Strings and Regular Expressions
Chapter 19 PHP Part II Credits: Parts of the slides are based on slides created by textbook authors, P.J. Deitel and H. M. Deitel by Prentice Hall ©
Scope, Objects, Strings, Numbers
Introduction to Scripting
JavaScript Syntax and Semantics
Section 3.2c Strings and Method Signatures
WEB PROGRAMMING JavaScript.
Advanced Find and Replace with Regular Expressions
elementary programming
JavaScript: Introduction to Scripting
Validation using Regular Expressions
Chapter 2 Primitive Data Types and Operations
ADVANCE FIND & REPLACE WITH REGULAR EXPRESSIONS
Presentation transcript:

RegExp

Regular Expression A regular expression is a certain way to describe a pattern of characters. Pattern-matching or keyword search. Regular expressions are frequently used to test whether or not a string entered in an HTML form has a certain format. For example, you want to test if a user entered a string that contains 3 consecutive digits. \d\d\d

Regular expression in JavaScript JavaScript provides regular expression capabilities through instances of the built-in RegExp object. var regTest = new RegExp(pattern, modifiers); Pattern specifies the patthern of an expression Modifiers specify if a search should be global, case-senstive The i modifier is used to perform case-insensitive matching The g modifier is used to perform a global match (find all matches rather than stopping after the first match) match() is a method of a String instance returns true if its string matches the pattern; otherwise, returns false.

macth() in JavaScript match() is a method of a String instance returns true if its string matches the pattern; otherwise, returns false. For example: var word = “I am a FSU student”; var re = /fsu/i; if(word.match(re)) alert(“Yes”); else alert(“No”);

test() test(argument) is a method of a Regular Expression instance returns true if the argument contains the pattern; otherwise, returns false. for example: var phoneNum = “ ”; var regTest = new RegExp(“^\\d{10}$”); if(regTest.test(phoneNum)){ alert(“Valid phone number”); } else{ alert(“Invalid phone number”);} What is the output?

Caret ^ and dollar sign $ ^ indicates the beginning of a string $ indicates the end of a string For example: \d\d\d represents strings consisting of three consecutive digits. It could represents all strings containing three consecutive digits, such as “my number is 123, blah blah”. ^\d\d\d$ represents strings consisting of only three consecutive digits.

Regular expression literal It is tiresome to write duplicate backslashes. var regTest = new RegExp(“^\\d\\d\\d$”); Alternative syntax for creating a RegExp instance is var regTest = /^\d\d\d$/; The expression on the right-hand side is known as regular expression literal. The scripting engine automatically escaping any backslash characters contained in the regular expression literal.

Special characters The simplest form of regular expression is a character that is not one of the regular expression special characters: ^ $ \. * + ? ( ) [ ] { } | A special character is escaped by preceding it with a backslash. For example, \$$ represents the set of strings that end with a dollar sign. A. means a character except for a line terminator, such as \n and tab. * is called Kleene star, represents infinitely large sets.

Escape Code A escape code regular expression represents multiple characters A list of escape code \d – digits : 0 through 9 \D – any character except those matched by \d \s – space: any JavaScript white space or line terminator ( space, tab, line feed, etc) \S – any character except those matched by \s \w – “word” character: any letter (a through z and A through Z), digit, or underscore \W – any character except those matched by \W

White space in regular expression A white space is significant within a regular expression For example, we have a regular expression, such as ^\d\. \w$ Does “3.A” match this regular expression? Does “9. B” match this regular expression?

Concatenation Operator Simple regular expressions can be composed into more complex regular expressions using concatenation operator. For instance: ^\d \s$ is a concatenated regular expression When you want to concatenate a regular expression with itself multiple times, you can use the quantifier shorthand notation. \d{3} == \d\d\d

Union Operator Union operator is represented by the pipe symbol | For example, \d|\s represents the set consisting of all digit and white space characters. Concatenation operator takes precedence over union, so \+|-\d|\s consists of +, the two-character strings beginning with – followed by a digit, and the white space characters.

Character class It is tedious to use the union operator to represent a set of all lowercase letters. JavaScript provides a character class that can be used for such purpose. [a-z] [A-Z] [0-9] How to represent \w using character class?

Examples of regular expression What does \d{3,6} represents? How about (\+|-){0,1}\d? How about \d*?