Regex in Visual Studio Code
Basic recipes for regular expression in editor
Last updated
Basic recipes for regular expression in editor
Last updated
VSCode primary support for regular expressions. Find and replace menu dialog can be opened with Ctrl + F
key combination.
It support plain text small, capital letters and regex.
We can search for new lines, carriage return, tabs, and spaces. Here for example, we would like to replace new line with comma (,) so that all the entries will be in csv.
The final result would look like this
With help of square brackets [ ], you can match for one (only one) of a set of characters.
Here the search goes for either of 1, 2, or 3 after immediately after # symbol.
More examples,
Important[:!,] searches for Important:
, Important!
or Important,
.
[AEIOUaeiou] searches for one of the letters in this set.
Important ✨
9 is a character, and 99 is a number with two characters 9 and 9.
Character ranges
For example for a long range of numbers or alphabets, it would be cumbersome. You can represent a range with a —
which acts as a metacharacter inside the square brackets []
and outside it acts as a literal character.
Number [0-9]
— matches number 0
to 9
.
Alphabets [A-Z]
— matches A
to Z
.
Small letter [a-z]
— matches a
to z
.
Exclude character
Search for words without the listed characters in the set. Here, ^
is the metacharacter for negation.
[^aeiou]
matches consonants
[^05]
matches the numbers not divisible by 5.
[^a-zA-Z]
matches other than alphabets.
For example,
medi[^a]
matches medium
Note 📝
] - ^ \
are not literals inside the[ ]
. But,. * ! , ; & $
are all the literals. This means, -a[sk.]txt
matchesa.txt
,astxt
,aktxt
only. - we can escape—
inside the brackets, asfile[0\-]
which also matches—
literal.