What is a regular expression
This article mainly shows you "what is a regular expression", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "what is a regular expression" this article.
What is a regular expression:
In a nutshell, regular expressions are a powerful tool for pattern matching and replacement. Find traces of regular expressions in almost all UNIX/LINUX-based software tools, such as Perl or PHP scripting languages. In addition, JavaScript, a client-side scripting language, also provides support for regular expressions, which have become a common concept and tool and are widely used by all kinds of technicians.
Basic syntax of regular expressions
First, create a JavaScript RegExp object
Var reg = RegExp ('s'); var reg = / sUnip; / / the shorthand method recommended cannot be empty or it will be regarded as a comment
2. Predefined character
Look at the meaning of some symbols before you begin to help you quickly understand the following example
\ s: space
\ s: not a space
\ d: number
\ d: non-numeric
\ W: character
\ W: non-character
\ I: case-insensitive
\ g: global matching (usually the rule will stop when it finds the first one that meets the condition, and adding this match after it will tell the rule to keep going.)
|: or
. : any character
\ b: separate parts (start, end, space)
\ B: non-independent part
\ n: a duplicate child, for example
Var reg = / (a) (b) (c)\ 1Universe; / / = > abca repeats a subitem
To use real symbols such as ".", simply add\, such as\.
3. Several commonly used methods
1. Test () = > find the content in the string that conforms to the rule specified. If found, return true, otherwise return false.
/ / usage: regular .test (string) var data = '123456789987654321 character reg = /\ d reg; / /\ d represents the number if (reg.test (str)) {console.log (' content is numeric'); console.log (reg.test (str)) / / returns true}
2. Match () = > query the content in the string in accordance with the regular specified content, and return the content (array format) if successful, otherwise return null
/ / usage: the string .match (regular) var data = '123456mple789Mple875654); / mple/gi;console.log (data.match (reg)); / / mple,Mple
3. Search () = > query the content specified by the rule in the string. If it is found successfully, the location of the current content will be returned from 0 (if more than one content meets the regular condition, the first location to be found will be returned). If not found,-1 will be returned.
/ / usage: string .search (regular) var data = '1234mple56789Mple987mple654321potential reg = / mple/gi;console.log (data.search (reg)); / / 4
4. Replace () = > query the content specified by the rule in the string, replace the corresponding content if found, and return the replaced content
/ usage: string .replace (regular, new string / callback function) var data ='la-la
/ / usage: regular .exec (string) var data = "1234mple5678mple99876mple543Mple21"; var reg = / mple/ig;var s = reg.exec (data) console.log (s.index); / / 4
6. Split () = regular split string
7. Sort (): the sorting method in the array, sorted by ACALL code
8. Join (): a method in an array that converts an array to a string
Var data = '4445554654123156489151321456'); data = arr.sort (). Join (''); console.log (data)
IV. Commonly used words
{nrecoery m}: at least n times, m times at most
{n,}: at least n times
*: any time is equivalent to {0,}
? Zero times or one time is equivalent to {0pm 1}
+: the result of the search appears at least once or any time {1,}
{n}: exactly n times
Finally, let's give an example to understand and judge the QQ number.
HTML
Detection
Regular pattern
/ / first, let's take a look at the nature of the QQ number we log in every day. 1 the first digit must not be 0 2. It must be a number of 5-10 digits var oInput = document.querySelector ('.qq'); var oSub = document.querySelector ('.sub'); var reg = / ^ [1-9]\ d {4jue 9} $/ / / in order to prevent situations such as the letter abc in the last few digits, you need to add $to limit the Mantissa / / so the final logic is like this, the first digit is 0-9, followed by 4-9 digit types. OSub.onclick = function () {if (reg.test (oInput.value)) {alert ('detected successfully');} else {alert ('account does not exist');}}. This is all the content of the article "what is a regular expression?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!