JavaScript Regular Expression Object

The RegExp object is used to specify what to search for in a text What is RegExp RegExp, is short for regular expression. When you search...


The RegExp object is used to specify what to search for in a text

What is RegExp

RegExp, is short for regular expression.
When you search in a text, you can use a pattern to describe what you are searching for. RegExp IS this pattern.

A simple pattern can be a single character.
A more complicated pattern consists of more characters, and can be used for parsing, format checking, substitution and more.
You can specify where in the string to search, what type of characters to search for, and more.

Defining RegExp

The RegExp object is used to store the search pattern.
We define a RegExp object with the new keyword. The following code line defines a RegExp object called patt1 with the pattern "e":

var patt1=new RegExp("e");

When you use this RegExp object to search in a string, you will find the letter "e".


Methods of the RegExp Object

The RegExp Object has 3 methods: test(), exec(), and compile().

test()

The test() method searches a string for a specified value. Returns true or false

Example:

var patt1=new RegExp("e");
document.write(patt1.test("The best things in life are free"));

Since there is an "e" in the string, the output of the code above will be:

true

exec()

The exec() method searches a string for a specified value. Returns the text of the found value. If no match is found, it returns null

Example 1:

var patt1=new RegExp("e");
document.write(patt1.exec("The best things in life are free"));

Since there is an "e" in the string, the output of the code above will be:

e

Example 2:

You can add a second parameter to the RegExp object, to specify your search. For example; if you want to find all occurrences of a character, you can use the "g" parameter ("global").
For a complete list of how to modify your search, visit our complete RegExp object reference.

When using the "g" parameter, the exec() method works like this:
  • Finds the first occurence of "e", and stores its position
  • If you run exec() again, it starts at the stored position, and finds the next occurence of "e", and stores its position
var patt1=new RegExp("e","g");
do
{
result=patt1.exec("The best things in life are free");
document.write(result);
}
while (result!=null)

Since there is six "e" letters in the string, the output of the code above will be:

eeeeeenull

compile()

The compile() method is used to change the RegExp.
compile() can change both the search pattern, and add or remove the second parameter.

Example:



var patt1=new RegExp("e");
document.write(patt1.test("The best things in life are free"));< patt1.compile("d"); document.write(patt1.test("The best things in life are free"));

Since there is an "e" in the string, but not a "d", the output of the code above will be:

truefalse

Complete RegExp Object Reference

For a complete reference of all the properties and methods that can be used with the RegExp object, go to our complete RegExp object reference.

The reference contains a brief description and examples of use for each property and method including the string object
Name

ADO,131,ASP,3,C++,61,CORE JAVA,1,CSS,115,HTML,297,index,5,JAVASCRIPT,210,OS,47,PHP,65,SAD,53,SERVLETS,23,SOFTWARE ENGINEERING,245,SQL,71,TCP/IP,1,XHTML,9,XML,18,
ltr
item
Best Online Tutorials | Source codes | Programming Languages: JavaScript Regular Expression Object
JavaScript Regular Expression Object
Best Online Tutorials | Source codes | Programming Languages
https://www.1000sourcecodes.com/2012/05/javascript-regular-expression-object.html
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/2012/05/javascript-regular-expression-object.html
true
357226456970214079
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content