- ES6 - Discussion
- ES6 - Useful Resources
- ES6 - Quick Guide
- ES9 - New Features
- ES8 - New Features
- ES7 - New Features
- ES6 - Browsers
- ES6 - Image Map
- ES6 - Debugging
- ES6 - Multimedia
- ES6 - Animation
- ES6 - Validations
- ES6 - Proxy API
- ES6 - Reflect API
- ES6 - Object Extensions
- ES6 - Error Handling
- ES6 - Modules
- ES6 - Promises
- ES6 - Maps And Sets
- ES6 - Classes
- ES6 - Collections
- ES6 - Iterator
- ES6 - HTML DOM
- ES6 - RegExp
- ES6 - Math
- ES6 - Date
- ES6 - Arrays
- ES6 - New String Methods
- ES6 - Symbol
- ES6 - Strings
- ES6 - Boolean
- ES6 - Number
- ES6 - Objects
- ES6 - Page Printing
- ES6 - Void Keyword
- ES6 - Dialog Boxes
- ES6 - Page Redirect
- ES6 - Cookies
- ES6 - Events
- ES6 - Functions
- ES6 - Loops
- ES6 - Decision Making
- ES6 - Operators
- ES6 - Variables
- ES6 - Syntax
- ES6 - Environment
- ES6 - Overview
- ES6 - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
ES6 - RegExp
A regular expression is an object that describes a pattern of characters. Regular expressions are often abbreviated “regex” or “regexp”.
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 the text.
A regular expression can be defined as −
var pattern = new RegExp(pattern, attributes); OR var pattern = /pattern/attributes;
The attribute can have any combination of the following values.
Sr.No | Attribute & Description |
---|---|
1 |
G Global Match |
2 |
I Ignore case |
3 |
M Multipne; treat the beginning and end characters (^ and $) as working over multiple pnes (i.e., match the beginning or the end of each pne (depmited by or ), not only the very beginning or end of the whole input string) |
4 |
U Unicode; treat the pattern as a sequence of unicode code points |
5 |
Y Sticky; matches only from the index indicated by the lastIndex property of this regular expression in the target string (and does not attempt to match from any later indexes) |
Constructing Regular Expressions
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 the bracketed character sequences and the 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 at least one p. |
2 |
p* It matches any string containing zero or more p s |
3 |
p? It matches any string containing one or more p s |
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 |
9 |
[^a-zA-Z] It matches any string not containing any of the characters ranging from a through z and A through Z |
10 |
p.p It matches any string containing p, followed by any character, in turn followed by another p |
11 |
^.{2}$ It matches any string containing exactly two characters |
12 |
<b>(.*)</b> It matches any string enclosed within <b> and </b> |
13 |
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 |
|