WebStorm 2021.2 Help

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.一旦您学习了正则表达式语法,就可以将其用于几乎任何语言。

  1. Press Ctrl+R to open the search and replace pane.Ctrl+R打开“搜索和替换”窗格。

  2. Enter a search string in the top field and a replace string in the bottom field.在顶部字段中输入搜索字符串,在底部字段中输入替换字符串。

    Regex search and replace fields

    Click the Regex icon to enable regular expressions. 单击the Regex icon以启用正则表达式。If you want to check the synax of regular expressions, hover over the Regex icon and click the Show expressions help link.如果要检查正则表达式的synax,请将鼠标悬停在the Regex icon上面,然后单击“显示表达式帮助”链接。

  3. When you search for a text string that contains special regex symbols, WebStorm automatically escapes them with backlash \ in the search field.当您搜索包含特殊正则表达式符号的文本字符串时,WebStorm会在搜索字段中使用反斜杠\自动将其转义。

    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.例如,如果需要查找.,请在搜索字段中键入\.

  4. 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.要搜索和替换更复杂的图案,请使用结构搜索和替换

  5. If the Match Case icon is unselected in the search field, WebStorm searches for both lower and upper cases.如果在搜索字段中未选中the Match Case icon,WebStorm将同时搜索小写和大写。

    The result of search with Match Case off

    Select the Match Case button in the search field to match the case of the specified range.在搜索字段中选择the Match Case button以匹配指定范围的大小写。

    The result of the Match Case selection

  6. When you browse the occurrences, WebStorm displays the replacement hints, so you can view the potential results before clicking the Replace button.浏览出现时,WebStorm会显示替换提示,因此您可以在单击“替换”按钮之前查看潜在结果。

    Replacement hints

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:例如,对于编号的捕获组,请使用以下语法:

Numbered regex capturing group

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

Named regex capturing group

Find and replace a captured group查找并替换捕获的组

Let's consider the following code:让我们考虑下面的代码:

gotoDetail(): void { this.router.navigate(['/detail', this.selectedHero.id]); }
  1. Open the search and replace pane Ctrl+R.打开“搜索和替换”窗格Ctrl+R

  2. In the search field, enter parentheses () that would indicate a capturing group, for example: '([^']+)detail'.在搜索字段中,输入表示捕获组的括号(),例如:'([^']+)detail'

  3. In the replace field, backreference such groups by numbers starting with 1, for example: 在“替换”字段中,通过以1开头的数字反向引用这些组,例如:'$1details'

  4. WebStorm highlights the found occurrences based on your search specifications and displays hints with the replace string.WebStorm根据您的搜索规范突出显示找到的事件,并显示带有替换字符串的提示。

    Replace with regex result

Switch the character case切换字符大小写

You can use regular expressions to change the case of characters that matches some criteria.可以使用正则表达式更改符合某些条件的字符的大小写。

  1. Open the search and replace pane Ctrl+R. 打开“搜索和替换”窗格Ctrl+RMake sure that the Regex icon is selected in the search field.确保在搜索字段中选择了the Regex icon

  2. In the search field enter the search pattern.在搜索字段中输入搜索模式。

  3. 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 becomes bar.例如,Bar变为bar

    • \u changes a character to uppercase until the next character in the string. 将字符更改为大写,直到字符串中的下一个字符。
      For example, bar becomes Bar.例如,Bar变为bar

    • \L changes characters to lowercase until the end of the literal string \E. 将字符更改为小写,直到文字字符串\E结束。
      For example, BAR becomes bar.例如,BAR变为bar

    • \U changes characters to uppercase until the end of the literal string \E. 将字符更改为大写,直到文字字符串\E结束。
      For example, bar becomes BAR.例如,bar变为BAR

    Switch to the uppercase character example
Last modified: 08 March 2021