Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common uses of regular expressions

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly shows you "what are the common uses of regular expressions". The content is simple and clear, and I hope it can help you solve your doubts. Let me lead you to study and learn the article "what are the common uses of regular expressions".

Basic concepts of regular expressions:

Regular expression, also known as regular representation, conventional representation. (English: Regular Expression, often abbreviated as regex, regexp or RE in code), a concept in computer science. Regular expressions use a single string to describe and match a series that conforms to a syntactic rule. In many text editors, regular expressions are often used to retrieve and replace text that matches a certain pattern.

Regular expression, there is no one like me, learned several times but still very ignorant, always understand when learning, forget all after learning. Well, in fact, it is not enough to practice, the so-called review the old and learn the new, can be a teacher, today with me to review this arrogant regular expression bar.

Why should there be regular expressions? In fact, it is because the computer is stupid (this is not what I said), such as 123456@qq.com, we can see that it is a mailbox, but the computer does not know it, so we have to use some languages that the computer knows to formulate good rules and tell it that what conforms to this rule is a mailbox, so that the computer can help us find the corresponding thing. So rules are used to set rules to complete some of the operations we need, such as login verification, search for specified things, and so on. It is superfluous to say too much, so let's get to the point.

Define regularities:

1 var re = new RegExp ("a"); / / RegExp object. Parameters are the rules we want to make. There is a situation that must be done in this way, which will be mentioned below. 2 var re = / a stroke; / / the abbreviated method is recommended for better performance and cannot be left empty, otherwise it is regarded as a comment

Common methods of regularization

1 test (): looks for regular content in the string. If it returns true, it returns false.

Usage: regular .test (string)

Example: determine whether it is a number or not

Var str = '374829348791 alternate re = /\ D str; / /\ D represents a non-numeric if (re.test (str)) {/ / returns a true, which means that a non-numeric number was found in the string. Alert ('not all numbers');} else {alert ('all numbers');}

There are many symbols in regular expressions that represent different meanings, which allow us to define different rules, such as the above\ D, and the following:

\ s: space

\ s: not a space

\ d: number

\ d: non-numeric

\ w: characters (letters, numbers, underscores _)

\ W: non-character example: whether there are characters that are not numbers

(the following will talk about some commonly used characters in turn according to the examples, and then make a summary. )

2 search (): search for regular content in a string and return the location where it appears (starting from 0, if more than one letter is matched, only the position of the first letter will be returned), and-1 will be returned if the search fails

Usage: string .search (regular)

Look for the contents of the compound regular in the string. Ignore case: i--ignore (the default in the rule is case-sensitive. If it is not case-sensitive, mark I at the end of the rule)

Example: find the letter b in a string without case sensitivity

Var str = 'abcdef';var re = / Bax re = new RegExp; you can also write alert (str.search (re)); / / 1

3 match () searches the string for the content of the compound rule. If the search succeeds, it returns the content in the format of an array, and returns null if it fails.

Usage: string .match (regular)

Quantifier: + the number of times that there is at least one uncertain match (match means search)

Global match: g--global (default in the rule, the search will end as soon as the content of the compound rule is found)

Example: find all the numbers in the specified format, as follows: find 123, 54, and 33879.

Var str = 'haj123sdk54hask33dkhalsd879'

Var re = /\ dmatching big g; / / matches at least one number at a time and if the global match is not a global match, it stops when the number 123 is found. It will only pop up 123. Add the global match, and you will search for those that match the rules from the beginning to the end. If there is no plus sign, the result of the match is that 1meme, 2jor3, pyror5, 4pyror3, pyrr3879 is not what we want. With the plus sign, the matching number is at least one at a time.

Alert (str.match (re)); / / [123 record54 33879]

4 replace (): look for a string that matches the rule and replace it with the corresponding string. Returns the replaced content.

Usage: string .replace (regular, new string / callback function) (in the callback function, the first argument refers to the character that matches successfully each time)

|: or.

Example: sensitive words filter, such as I love Tiananmen Square in Beijing, the sun rises on Tiananmen Square. -I love the sun rising on *. That is, Beijing and Tiananmen Square become *.

At first we might think of something like this:

Var str = "I love Tiananmen Square in Beijing, where the sun rises." ; var re = / Beijing | Tiananmen Square / g; / / find Beijing or Tiananmen global match var str2 = str.replace (re,'*'); alert (str2) / / I love * *, * Sunrise / / this just turns what you find into a *, not just a few words.

To implement several words corresponding to several *, we can use the callback function to implement:

Var str = "I love Tiananmen Square in Beijing, where the sun rises." ; var re = / Beijing | Tiananmen Square / g; / find Beijing or Tiananmen global match var str2 = str.replace (re,function (str) {alert (str); / / used to test: the first parameter of the function represents the regular characters searched each time, so the first str refers to Beijing's second str is Tiananmen Square and the third str is Tiananmen var result ='; for (Tiananmen var result ='; for / / match at least one non-closing parenthesis in the middle of the left parenthesis (because there are attributes and so on in the tag), and then match the right parenthesis

Var re = / / g; / / matches at least one character or non-character in the middle of the left parenthesis, and then matches the right parenthesis / / is actually finding the left parenthesis, and then there can be at least one content in the middle until the right parenthesis is found to represent a label.

Escape character

\ s: space

\ s: not a space

\ d: number

\ d: non-numeric

\ w: characters (letters, numbers, underscores _)

\ W: non-character

. (dot)-any character

\. The real point

\ b: separate parts (start, end, space)

\ B: non-independent part

About the last two, let's take a look at a chestnut:

Var str = 'onetwo';var str2 = "onetwo"; var re = / one\ b str2; / / e must be followed by an independent start, space, or end alert (re.test (str)); / / falsealert (re.test (str2)); / / true

Example: write a function that gets the node with the class name:

We may have seen functions like this before:

Function getByClass (parent,classname) {if (parent.getElementsByClassName) {return parent.getElementsByClassName (classname);} else {var results = new Array (); / / it is used to store all fetched elements var elems = parent.getElementsByTagName ("*") with class of box; for (var I = 0twiti)

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: 0

*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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report