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

Examples of regular expressions in VSCode

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

Share

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

Xiaobian to share with you the VSCode regular expression examples, I hope you have something to gain after reading this article, let's discuss it together!

Text Editor Settings

Although almost all text editors now support regular expressions, I used Visual Studio Code for this tutorial, but you can use any editor you like. Also note that you usually need to turn on the RegEx switch somewhere near the search input box. Here is how to do this in VSCode:

You need to enable RegEx by checking this option

1) .  -  Match any character

Let's get started. Dot symbol. Used to match any character:

b.t

The above regulars match "bot,"`"bat," and any three-character word that starts with b and ends with t. But if you want to search for a dot symbol, you need to escape it with\, so the following regular matches only the exact text "b.t":

b\.t

2) .*  - Match anything. 

here. For "any character,"* means "this symbol repeats the previous one any number of times." "Put them together (... *) Means "any symbol repeated any number of times. "For example, you can use it to find matches that start or end with some text. Suppose we have a javascript method like this:

loadScript(scriptName: string, pathToFile: string)

We want to find all calls to this method where pathToFile points to any file in the folder "lua." You can use the following regular expressions:

loadScript.* lua

This means that "matches all strings that start with"loadScript"and end with"lua." "

3) ?  -  nongreedy matching

.* After that? Symbols and other matching rules mean "as few matches as possible." In the previous image, each match resulted in two "lua" strings, and everything didn't match until the second "lua." If you want to match the first occurrence of "lua," you can use the following regular:

loadScript.*? lua

This means,"Match everything that starts with"loadScript"followed by any character until the first occurrence of"lua"

loadScript.*? lua: Matches everything starting with loadScript until the first occurrence of "lua"

4)`( ) ###-Capture groups and backreferences 

Okay, now we can match some text. But what if you want to modify some of the text we found? This is where the capture group is used.

Suppose we modify the loadScript method so that we now need to insert an additional parameter between its original two parameters. Let's name this new parameter id, and the new function prototype should look like this: loadScript(scriptName, id, pathToFile). We can't use the regular substitution features of text editors here, but regular expressions can help us.

You can see the result of running the following regular expression:

loadScript\(.*?,.*?\)

This means: "Match anything that starts with"loadScript(", followed by anything until the first, then anything until the first)"

The only thing that might seem strange to you is the\symbol. They are used to escape parentheses.

Because the symbols (and) are special characters that regular expressions use to capture matching text parts, but we need to match actual parenthesis characters, we need to escape them.

In the previous expression, we used.*? The symbol defines two parameters to a method call. To make each parameter a separate capture group, add (and) symbols before and after them:

loadScript\((.*?), (.*?)\)

If you run this regular, you'll see nothing changes. This is because it matches the same text. But now we can call the first argument\$1 and the second argument\$2. This is called a backreference, and it will help us do what we want: add another parameter between two parameters:

Search input:

loadScript\((.*?), (.*?)\)

This is the same as the previous regularization, but maps parameters to inverted capture groups 1 and 2, respectively.

Alternative inputs:

loadScript($1,id,$2)

This means "Replace each matching text with the text"loadScript(", Capture Group 1,"id", Capture Group 2, and)." Note that you do not need to escape parentheses in substitution input.

5)[ ]-Character class  

You can list the characters to match at specific positions inside the [and] symbol. For example,[0-9] matches all numbers from 0 to 9. You can also list all the numbers explicitly: [0123456789] --same meaning as before. You can also use dashes with letters,[a-z] will match all lower-case Latin characters,[A-Z] will match all upper-case Latin characters,[a-zA-Z] will match both.

You can also use * after character classes, just like in. And then again, which in this case means: "Match any number of characters in this class"

After reading this article, I believe you have a certain understanding of "VSCode regular expression examples", if you want to know more about the relevant knowledge, welcome to pay attention to the industry information channel, thank you for reading!

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