How To Do A Multiline Regex Search In VSCode

Multiline Regex Search In Visual Studio Code

Microsoft Visual Studio Code is one of the most popular code editors for developers. But its utility extends much beyond that. However for the sake of this article, we are going to limit it to executing multiline regular expression search functionality. Let’s just call it VSCode for short.

VSCode has a beautiful regex search feature. For most use-cases it works perfectly. However how do you execute a multiline search?

For eg. a very vague pcre regex like /.*keyword.*/s means “match all text that contains the keyword” because the /s modifier stands for PCRE_DOTALL and makes the . dot metacharacter to extend to including newlines.

If this modifier is set, a dot metacharacter in the pattern matches all characters, including newlines. Without it, newlines are excluded. This modifier is equivalent to Perl’s /s modifier. A negative class such as [^a] always matches a newline character, independent of the setting of this modifier.

Multiline Regular Expressions in Microsoft Visual Studio Code

How do you achieve the same functionality in VSCode? The following modifiers make VSCode match the entire text containing the keyword:
[\s\S\n]*keyword[\s\S\n]*

Explanation of the Regular Expression:

\s: matches any whitespace character (space, table, line breaks)

\S: matches any character that is not a whitespace character

\n: matches a line feed character (code 10)

Pro Tip: When typing regular expressions in VSCode’s search field, turn off the regular expression button because VSCode starts matching as soons as you start typing and can result in a crash. Once you have completed typing, turn on the regular expression button to trigger the search.

Divi WordPress Theme