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

JavaScript Enhancement Tutorial - RegExp Object

巴扎黑
Release: 2016-11-25 15:19:50
Original
982 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 a certain 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.

Define RegExp

RegExp object is used to store retrieval patterns.

Use the new keyword to define the RegExp object. 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 RegExp object

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

test()

test() method retrieves a specified value in 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")); Since the letter "e" exists in the string, the output of the above code Will be: true

exec()

exec() method retrieves the specified value in the 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")); Since the letter "e" exists in the string, the above code The output will be: e

Example 2:

You can add a second parameter to the RegExp object to set the retrieval. 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, start retrieval from the stored position and find 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) Due to the 6 "e" letters in this string, the output of the code will be: eeeeeenull

compile()

compile() method is used to change RegExp.

compile() can not only change the retrieval mode, but also 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 "e" exists in the string but not "d", the output of the above code is: truefalse


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