- Javascript - Page Printing
- Javascript - Void Keyword
- Javascript - Dialog Boxes
- Javascript - Page Redirect
- Javascript - Cookies
- Javascript - Events
- Javascript - Functions
- Javascript - Loop Control
- Javascript - For...in
- Javascript - For Loop
- Javascript - While Loop
- Javascript - Switch Case
- Javascript - If...Else
- Javascript - Operators
- Javascript - Variables
- Javascript - Placement
- Javascript - Enabling
- Javascript - Syntax
- Javascript - Overview
- Javascript - Home
JavaScript Objects
- Javascript - HTML DOM
- Javascript - RegExp
- Javascript - Math
- Javascript - Date
- Javascript - Arrays
- Javascript - Strings
- Javascript - Boolean
- Javascript - Number
- Javascript - Objects
JavaScript Advanced
- Javascript - Browsers
- Javascript - Image Map
- Javascript - Debugging
- Javascript - Multimedia
- Javascript - Animation
- Javascript - Validations
- Javascript - Error Handling
JavaScript Useful Resources
- Javascript - Resources
- Javascript - Functions
- Javascript - Quick Guide
- Javascript - Questions And Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Regular Expressions and RegExp Object
A regular expression is an object that describes a pattern of characters.
The JavaScript RegExp class represents regular expressions, and both String and RegExp define methods that use regular expressions to perform powerful pattern-matching and search-and-replace functions on text.
Syntax
A regular expression could be defined with the RegExp () constructor, as follows −
var pattern = new RegExp(pattern, attributes); or simply var pattern = /pattern/attributes;
Here is the description of the parameters −
pattern − A string that specifies the pattern of the regular expression or another regular expression.
attributes − An optional string containing any of the "g", "i", and "m" attributes that specify global, case-insensitive, and multi-pne matches, respectively.
Brackets
Brackets ([]) have a special meaning when used in the context of regular expressions. They are used to find 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. |
6 | [a-Z] It matches any character from lowercase 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.
Quantifiers
The frequency or position of bracketed character sequences and single characters can be denoted by a special character. Each special character has a specific connotation. The +, *, ?, and $ flags all follow a character sequence.
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. |
Examples
Following examples explain more about matching characters.
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 |