In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how JavaScript defines the content to be retrieved in text. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
JavaScript RegExp object
The RegExp (regular expression) object is used to specify what is retrieved in the text.
What is RegExp?
RegExp is the abbreviation of regular expression.
When you retrieve a text, you can use a pattern to describe what you want to retrieve. This is the mode of RegExp:
A simple pattern can be a single character.
More complex patterns include more characters and can be used for parsing, format checking, substitution, and so on.
You can specify the location in the string to retrieve, the type of characters to retrieve, and so on.
Define RegExp
The RegExp object is used to store the retrieval schema and the RegExp object is defined by the new keyword.
For example, the following code defines a RegExp object named patt1 with a pattern of "e":
Var patt1=new RegExp ("e")
Note: when you use this RegExp object to retrieve in a string, you will be looking for the character "e".
Methods of RegExp object
The RegExp object has three methods: test (), exec (), and compile ().
Test ()
The test () method retrieves the specified value from the string, and the return value is true or false:
Var patt1=new RegExp ("e"); [xss_clean] (patt1.test ("The best things in life are free"))
Note: because the letter "e" exists in the string, the output of the above code will be: true.
Exec ()
The exec () method retrieves the specified value in the string, and the return value is the value found, and if no match is found, it returns null:
Var patt1=new RegExp ("e"); [xss_clean] (patt1.exec ("The best things in life are free"))
Note: because the letter "e" exists in the string, the output of the above code will be: e.
Tip: you can add a second parameter to the RegExp object to set the retrieval.
For example, if you need to find all the existence of a character, you can use the "g" parameter ("global").
Var patt1=new RegExp ("e", "g"); do {result=patt1.exec ("The best things in life are free"); [xss_clean] (result);} while (resulting null)
Note: since there are six "e" letters in this string, the output of the code will be: eeeeeenull.
Tip: when using the "g" parameter, exec () works as follows:
Find the first "e" and store its location
If you run exec () again, retrieve it from the location where it was stored, find the next "e", and store its location.
Compile ()
The compile () method is used to change the RegExp.
Compile () can either change the retrieval mode or add or remove a second parameter.
Var patt1=new RegExp ("e"); [xss_clean] (patt1.test ("The best things in life are free")); patt1.compile ("d"); [xss_clean] (patt1.test ("The best things in life are free"))
Note: because there is an "e" instead of a "d" in the string, the output of the above code is: truefalse.
Properties and description of RegExp object
Whether the global:RegExp object has a flag g.
Whether the ignoreCase:RegExp object has a flag I.
LastIndex: an integer indicating the position of the character that starts the next match.
Whether the multiline:RegExp object has the flag m.
Source: the source text of the regular expression.
RegExp object method and description
Compile: compiles regular expressions.
Exec: retrieves the value specified in the string. Returns the value found and determines its location.
Test: retrieves the value specified in the string. Returns true or false.
Methods of String objects that support regular expressions
Search: retrieves the value that matches the regular expression.
Match: find a match for one or more regular expressions.
Replace: replaces the substring that matches the regular expression.
Split: split a string into an array of strings.
Thank you for reading! This is the end of the article on "how JavaScript regulates the content to be retrieved in the text". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 244
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.