Notepad++, Sublime Text Editor Replacement with Regular Expressions

Sublime Text Editor
Here is a question about which regex I can use in Sublime Text Editor to find all attributes in an opening tag, for example:
<h1 class="class-one class-two" data-level="2" data-pm-slice="1 1 []">
I want to replace it with:
<h1>
- Open the "Find and Replace" tool: Press
Ctrl+H
in Windows/Linux orCmd+Opt+F
on macOS. - Enable regular expressions: Click the
.*
button in the bottom left corner of the "Find and Replace" dialog to enable regex mode. - Enter the regex in the "Find what" field: Use this regex to precisely match any attributes inside
<h1>
tags:
(<h1)[^>]*(>)
- Explanation:
(<h1)
: Captures the opening<h1
tag into a group for reference in the replacement.[^>]*
: Matches all characters that are not closing>
, effectively capturing all attributes inside the tag.(>)
: Captures the closing>
tag into another group.
- Set the replacement text: In the "Replace with" field, use:
$1$2
- This replacement string uses
$1
and$2
to refer to the captured groups from the regex, effectively restoring the tag without its attributes. - Perform the replacement: Click "Replace All" to replace all found instances where
<h1>
tags have additional attributes, leaving you with plain<h1>
tags.
Notepad++
Sometimes laziness drives us to figure out things we didn't want to delve into before. It's easier to spend half an hour understanding automatic replacement with regular expressions than manually sifting through large text files and replacing everything by hand!
For example, we need to change
a href="any_URL"
to
a href="our_URL"
We denote the place of any URL using [^"]*, resulting in something like:
a href="[^"]*"
replace with
a href="our_URL"
That's it, just don't forget to check the "Regular Expressions" box.