Home > Web Front-end > JS Tutorial > JavaScript Enhancement Tutorial - RegExp Object

JavaScript Enhancement Tutorial - RegExp Object

黄舟
Release: 2017-01-21 15:56:30
Original
1210 people have browsed it

This article is the official HTML5 training tutorial of H5EDU organization. It mainly introduces: JavaScript enhancement tutorial--RegExp object

The RegExp object is used to specify the content to be retrieved in the text.

What is RegExp?

RegExp is the abbreviation of regular expression.

When you retrieve some text, you can use a pattern to describe what you want to retrieve. RegExp is this pattern.

Simple pattern can be a single character.

More complex patterns include more characters and can be used for parsing, format checking, replacement, etc.

You can specify the search position in the string, the type of characters to be searched, etc.

Definition RegExp

The RegExp object is used to store retrieval patterns.

Define the RegExp object through the new keyword. The following code defines a RegExp object named patt1 whose pattern is "e": var patt1=new RegExp("e");When you use this RegExp object to search within a string, you are looking for the character "e" ".

Methods of the RegExp object

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

test()

The test() method retrieves a specified value from a string. The return value is true or false.

Example:

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

Since the letter "e" exists in the string, the output of the above code will be:

true
Copy after login


exec()

The exec() method retrieves a specified value from a string. The return value is the found value. If no match is found, null is returned.

Example 1:

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

Since the letter "e" exists in the string, the output of the above code will be: e

Example 2:

You You can add a second parameter to the RegExp object to set up the search. For example, if you need to find all occurrences of a certain character, you can use the "g" parameter ("global").

For complete information on how to modify the search pattern, please visit our RegExp Object Reference Manual.

When using the "g" parameter, exec() works as follows:

Find the first "e" and store its position
If you run exec() again, Then start retrieval from the stored position, find the next "e", and store 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)
Copy after login

Since there are 6 "e" letters in this string, the output of the code will be:

eeeeeenull
Copy after login


compile()

compile() method is used to change RegExp.

compile() can not only change the retrieval mode, but also add or delete 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"));
Copy after login

Since "e" exists in the string but not "d", the output of the above code is: truefalse
Click to enter the JS enhancement tutorial

The above is the content of the JavaScript enhancement tutorial-RegExp object. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template