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

How to learn regular expressions quickly

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article is about how to learn regular expressions quickly. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Regular expression concept

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

In our daily work, we often write regular expressions. For example, when we often use regular expressions in forms to verify whether the format of user input is correct, we use regular expressions. We can think of regular expressions as languages that can describe problems. It is designed specifically for pattern matching.

Syntax of regular expressions

You can simply and rudely understand a regular expression as finding something. For example, to find the word "world" in "Hello World", first use straightforward language to describe it as finding "letter combinations from w to d", and use rules to describe it as\ bw\ Widd\ b. This is the pattern string, which consists of metacharacters and literal characters.

Common metacharacters are as follows:

Flags in regular expressions

Escape characters in regular expressions

Regular expressions in JS

Two ways of generation

For example: does the match 0-9 appear?

The copy code is as follows:

1. Through RegExp var reg=new RegExp ('^ [0-9]','g')

2. Var reg=/ ^ [0-9] / g literally through the expression

Two commonly used methods

Test (), exec ()

The copy code is as follows:

1. Test method: if the input string matches the pattern, test returns true, otherwise false

2. Exec method: if the input string matches the pattern, an array is returned. If there is no match, null is returned.

/ * test () method, which matches whether 0-9*/var reg1=/ ^ [0-9] /; reg1.test ('There is no number'); / / returns the false/*exec () method to match the words in the sentence. A group of two words * / / * does not set the global flag * / var reg2=/ (\ w+)\ s (\ w+) /; reg2.test (' There is no number') / / return the result ['There is','There','is'] / / the 0th element is a matching string, and the last two elements are the substring of the grouped reference / * set the global flag * / var reg3=/ (\ w+)\ s (\ w+) / gtwitreg3.test (' There is no number') / / if the global flag is set, exec () will loop to find / / the first search result ['There is','There','is'], the second result [' no number','no','number'], and the third result null exec () method. If the global flag is set, its reg.lastIndex must be set manually in the loop, otherwise it will match at intervals.

String objects and regular expressions

There are many methods that take regular expression objects as parameters in string objects.

Property description match (regexp) returns the matching result of the regular expression regexp replace (searchValue,replaceValue) will searchValue (regular expression or string value)

Replace with replaceValue and return the response string search (regexp) returns the subscript where the regular expression regexp matches. If there is no match, return-1split (separator,limit) through the parameter separator (string or regular expression)

Splits the string and returns an array of strings

The match method returns an array of strings whose elements match the pattern, and when the global flag is set, it returns all the string arrays that match the pattern, which is the same as the exec method when not set.

Application examples:

Var text='abc def ghi jkl';// sets the global flag text.match (/\ wagnogram); / / ["abc", "def", "ghi", "jkl"] / / does not set the global flag text.match (/ (\ w+)\ s (\ w+) /) / ["abc def", "abc", "def"] the replace method returns the replaced string. If the global flag is set, all matched strings are replaced, otherwise only the first matching string is replaced. In replace, if the first parameter uses a grouping, the second parameter can identify the grouped forward reference by symbol.

Application examples:

Var text= "abc def ghi jkl"; / / replace the space with the character text.replace (/\ s _ pachyg); / / "abc,def ghi jkl" text.replace (/\ s _ tag _ g); / / "abc,def,ghi,jkl" / / the grouping of characters before a pair of spaces, replace it with a comma and move forward one text.replace (/ (.)\ splagg, ", $1") / / "ab,cde,fgh,igkl" in the search and split methods, the global flag of the regular expression does not work. Thank you for reading! This is the end of the article on "how to learn regular expressions quickly". 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 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: 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