WebStorm 2020.3 Help

Structural search and replace examples

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

If you have a JavaScript or Typescript class MyClass:

class MyClass {}

Then the simplest template to search for it is class $a$.

Examples for HTML and XML

The following examples show how you can use structural search in HTML and XML code.

Searching for XML and HTML tags, attributes, and their values

  • The simplest template to search for a tag is <$tag$/>.

    By placing constraints on the variable $tag$, you can specify tags that you want to find. For example, if you specify li, you will all li tags.

    Consider the following template for searching in XML and 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.

    Searching for li tags with numeric ids

Delete all lines that have the id attribute greater than 2

  1. In the Search template field, we create a template that searches for all li tags with numeric values (\d+) in id attributes. 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. The script reads the content of the d variable and returns it as a string (for example, "1"). Then the script replaces all quotes and converts the string value to integer and compares it with 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

  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: 08 May 2020