- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create RegExp object in JavaScript?
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 the text.
A regular expression could be defined with the RegExp () constructor, as follows −
var pattern = new RegExp(pattern, attributes); or simply var pattern = /pattern/attributes;
The following are 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 multiline matches, respectively.
Example
You can try to run the following code to learn how to implement RegExp object in JavaScript −
<html> <head> <title>JavaScript RegExp</title> </head> <body> <script> var re = new RegExp( "string" ); document.write("re.constructor is:" + re.constructor); </script> </body> </html>
Advertisements