Regular Expression in Javascript Regular Expression
Regular Expression in Javascript Regular Expression
2. Regular Expression
• 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
• 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.
4. 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 re = /fsu/i;
if(word.match(re))
alert(“Yes”);
else alert(“No”);
5. test()
• test(argument) is a method of a Regular Expression instance returns true if the
argument contains the pattern; otherwise, returns false.
for example:
if(regTest.test(phoneNum)){
else{
6.
Caret ^ and dollar sign $
• For example:
• varregTest = /^\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.
8. 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.
9. Escape Code
• \s – space: any JavaScript white space or line terminator ( space, tab, line feed, etc)
• \w – “word” character: any letter (a through z and A through Z), digit , or underscore
• For example,
we have a regular expression, such as ^\d\. \w$ • Does “3.A” match this regular
expression?
• Simple regular expressions can be composed into more complex regular expressions
using concatenation operator.
• When you want to concatenate a regular expression with itself multiple times, you
can use the quantifier shorthand notation.
• \d{3} == \d\d\d
• 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.
• 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.
/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
Character Description
\w+ Matches one or more word characters including the underscore. Equivalent to [A-Za-z0-9_].
[\.-] \ Indicates that the next character is special and not to be interpreted literally.
.- matches character . or -.
\w+ Matches 1 or more word characters including the underscore. Equivalent to [A-Za-z0-9_].
\w+ The sub-expression \w+([.-]?\w+)* is used to match the username in the email. It begins
([.-]?\w+)* with at least one or more word characters including the underscore, equivalent to [A-Za-z0-
9_]. , followed by . or - and . or - must follow by a word character (A-Za-z0-9_).
\w+ It matches the domain name with the same pattern of user name described above..
([.-]?\w+)*
\.\w{2,3} It matches a . followed by two or three word characters, e.g., .edu, .org, .com, .uk, .us, .co
etc.
+ The + sign specifies that the above sub-expression shall occur one or more times, e.g., .com,
.co.us, .edu.uk etc.