Find and replace text using regular expressions使用正则表达式查找和替换文本
When you want to search and replace specific patterns of text, use regular expressions. 如果要搜索和替换特定的文本模式,请使用正则表达式。They can help you in pattern matching, parsing, filtering of results, and so on. 它们可以帮助您进行模式匹配、解析、结果过滤等。Once you learn the regex syntax, you can use it for almost any language.一旦您学习了正则表达式语法,就可以将其用于几乎任何语言。
-
Press Ctrl+R to open the search and replace pane.按Ctrl+R打开“搜索和替换”窗格。 -
Enter a search string in the top field and a replace string in the bottom field.在顶部字段中输入搜索字符串,在底部字段中输入替换字符串。Click单击to enable regular expressions.
以启用正则表达式。
If you want to check the synax of regular expressions, hover over如果要检查正则表达式的synax,请将鼠标悬停在and click the Show expressions help link.
上面,然后单击“显示表达式帮助”链接。
-
When you search for a text string that contains special regex symbols, WebStorm automatically escapes them with backlash当您搜索包含特殊正则表达式符号的文本字符串时,WebStorm会在搜索字段中使用反斜杠\
in the search field.\
自动将其转义。However, when you specifically search for metacharacters such as但是,当您专门搜索元字符(如.[{()\^$|?*+
, you need to escape them with backslash\
, so they can be recognized..[{()\^$|?*+
时,需要使用反斜杠\
对其进行转义,以便识别它们。For example, if you need to find例如,如果需要查找.
, type\.
in the search field..
,请在搜索字段中键入\.
。 -
WebStorm can also match a letter case when you enter a range of characters in your search field.当您在搜索字段中输入一系列字符时,WebStorm还可以匹配字母大小写。For example, if you want to search for only uppercase characters, type the following in the search field:例如,如果要仅搜索大写字符,请在搜索字段中键入以下内容:\b[A-Z]To search and replace more complicated patterns, use the structural search and replace.要搜索和替换更复杂的图案,请使用结构搜索和替换。 -
If如果在搜索字段中未选中is unselected in the search field, WebStorm searches for both lower and upper cases.
,WebStorm将同时搜索小写和大写。
Select在搜索字段中选择in the search field to match the case of the specified range.
以匹配指定范围的大小写。
-
When you browse the occurrences, WebStorm displays the replacement hints, so you can view the potential results before clicking the Replace button.浏览出现时,WebStorm会显示替换提示,因此您可以在单击“替换”按钮之前查看潜在结果。
Use regex capturing groups and backreferences使用正则表达式捕获组和反向引用
You can put the regular expressions inside brackets in order to group them. 可以将正则表达式放在括号内,以便对它们进行分组。Each group has a number starting with 1, so you can refer to (backreference) them in your replace pattern. 每个组都有一个以1开头的数字,因此您可以在替换模式中引用(反向引用)它们。Note that the group 0 refers to the entire regular expression. 请注意,组0引用整个正则表达式。However, you can refer to the captured group not only by a number 但是,您不仅可以通过数字$n
, but also by a name ${name}
.$n
引用捕获的组,还可以通过名称${name}
引用捕获的组。
For example, for the numbered capturing groups, use the following syntax:例如,对于编号的捕获组,请使用以下语法:

For the named capturing groups, use the following syntax:对于命名的捕获组,请使用以下语法:

Find and replace a captured group查找并替换捕获的组
Let's consider the following code:让我们考虑下面的代码:
-
Open the search and replace pane Ctrl+R.打开“搜索和替换”窗格Ctrl+R
。 -
In the search field, enter parentheses在搜索字段中,输入表示捕获组的括号()
that would indicate a capturing group, for example:'([^']+)detail'
.()
,例如:'([^']+)detail'
。 -
In the replace field, backreference such groups by numbers starting with 1, for example:在“替换”字段中,通过以1开头的数字反向引用这些组,例如:'$1details'
-
WebStorm highlights the found occurrences based on your search specifications and displays hints with the replace string.WebStorm根据您的搜索规范突出显示找到的事件,并显示带有替换字符串的提示。
Switch the character case切换字符大小写
You can use regular expressions to change the case of characters that matches some criteria.可以使用正则表达式更改符合某些条件的字符的大小写。
-
Open the search and replace pane Ctrl+R.打开“搜索和替换”窗格Ctrl+R。Make sure that确保在搜索字段中选择了is selected in the search field.
。
-
In the search field enter the search pattern.在搜索字段中输入搜索模式。 -
In the replace field, depending on what you want to achieve, enter one of the following syntax:在“替换”字段中,根据要实现的目标,输入以下语法之一:-
\l
changes a character to lowercase until the next character in the string.将字符更改为小写,直到字符串中的下一个字符。For example,例如,Bar
becomesbar
.Bar
变为bar
。 -
\u
changes a character to uppercase until the next character in the string.将字符更改为大写,直到字符串中的下一个字符。For example,例如,bar
becomesBar
.Bar
变为bar
。 -
\L
changes characters to lowercase until the end of the literal string将字符更改为小写,直到文字字符串\E
.\E
结束。
For example,例如,BAR
becomesbar
.BAR
变为bar
。 -
\U
changes characters to uppercase until the end of the literal string将字符更改为大写,直到文字字符串\E
.\E
结束。For example,例如,bar
becomesBAR
.bar
变为BAR
。
-