WebStorm 2021.2 Help

Structural search and replace examples结构搜索和替换示例

As you know the main difference between regular search and the structural search is that in the structural search we are looking for a structural template in a programming language.正如您所知,常规搜索和结构搜索之间的主要区别在于,在结构搜索中,我们正在寻找编程语言中的结构模板。

The beauty of a structural search is that you can create a pattern based on the existing template and save yourself time when searching and replacing code.结构化搜索的美妙之处在于,您可以基于现有模板创建模式,并在搜索和替换代码时节省时间。

The extensive list of existing templates covers a lot of use-cases from simple patterns to more complex ones.现有模板的广泛列表涵盖了从简单模式到更复杂模式的许多用例。

Each item in a pattern consists of variables that are limited by $ sign on both sides.模式中的每个项都由变量组成,这些变量在两侧都受到$符号的限制。

Method call方法调用

$Instance$.$MethodCall$($Parameter$)

This template matches method call expressions. 此模板与方法调用表达式匹配。If the number of occurrences is zero, it means that a method call can be omitted.如果出现次数为零,则表示可以忽略方法调用。

@Deprecated $Instance$.$MethodCall$($Parameter$)

You can use this template to find deprecated methods and use it as prototype for creating other annotated method templates. 您可以使用此模板查找不推荐使用的方法,并将其用作创建其他带注释方法模板的原型。Specifically for searching method calls to deprecated methods you can select the already existing template method calls to deprecated methods.特别是对于搜索不推荐方法的方法调用,您可以选择已存在的不推荐方法的模板方法调用

Refer to Structural search and replace on how to create a search template.有关如何创建搜索模板,请参阅结构搜索和替换

Searching for JavaScript and Typescript classes搜索JavaScript和Typescript类

If you have a JavaScript or Typescript class MyClass:如果您有JavaScript或Typescript类MyClass

class MyClass {}

Then the simplest template to search for it is class $a$.然后搜索它的最简单模板是class $a$

Examples for HTML and XMLHTML和XML示例

The following examples show how you can use structural search in HTML and XML code.以下示例演示如何在HTML和XML代码中使用结构化搜索。

Searching for XML and HTML tags, attributes, and their values搜索XML和HTML标记、属性及其值

  • The simplest template to search for a tag is <$tag$/>.搜索标记的最简单模板是<$tag$/>

    By placing constraints on the variable $tag$, you can specify tags that you want to find. 通过在变量$tag$上放置约束,可以指定要查找的标记。For example, if you specify li, you will all li tags.例如,如果指定li,则将显示所有li标记。

    Consider the following template for searching in XML and HTML: <$tag$ $attribute$=$value$ />. 考虑下面的模板在XML和HTML中搜索:<$tag$ $attribute$=$value$ />For example, if you specify the text filter id for the $attribute$ variable and the \d+ regular expression as the text filter for the $value$ variable, you can find all tags that have numeric values in the id attribute.例如,如果针对$attribute$变量指定文本筛选器id,针对$value$变量指定\d+正则表达式作为文本筛选器,则可以在id属性中找到所有具有数值的标记。

    Searching for li tags with numeric ids

Delete all lines that have the id attribute greater than 2删除id属性大于2的所有行

  1. In the Search template field, we create a template that searches for all li tags with numeric values (\d+) in id attributes. 在“搜索模板”字段中,我们创建一个模板,用于搜索id属性中具有数值(\d+)的所有li标记。We expand our search to the whole string with such values (Search target = Complete match).我们将搜索扩展到具有这些值的整个字符串(搜索目标=完全匹配)。

  2. We filter those lines with the following Groovy script: d.getText().replaceAll (/"/, '').toInteger() > 2. 我们使用以下Groovy脚本过滤这些行:d.getText().replaceAll (/"/, '').toInteger() > 2The script reads the content of the d variable and returns it as a string (for example, "1"). 脚本读取d变量的内容并将其作为字符串返回(例如"1")。Then the script replaces all quotes and converts the string value to integer and compares it with 2.然后脚本替换所有引号并将字符串值转换为整数,并将其与2进行比较。

  3. In the Replace template field, we put nothing to delete the whole string. After the search, we select Replace All to perform the replacing.

    Delete lines with ids greater than 2

Convert uppercase values of the class attribute in p tags to lowercase将p标记中class属性的大写值转换为小写

  1. In the Search template field, we create a template that searches for all p tags with uppercase values ([A-Z].* and Match case) in class attributes. We narrow our search only to these to class values (Search target = b).

  2. In the Replace template field, we create a new variable $d$ and assign a Groovy script to it (b.getText().toLowerCase()). After the search, we select Replace All to perform the replacing.

    Replace a target structurally
Last modified: 03 June 2021