Code Bytes
Find text between HTML tags in Sublime Text
Use a scoped regular-expression search for simple HTML edits, with examples for heading text and multiline comments and clear limits.
Find the text you actually want to edit
A regular expression is handy for a small, controlled edit in a known file. It is not an HTML parser. Preview every match before using Replace All, especially in templates or documents with embedded code.
Open Find, enable the .* regular-expression option, and try this on simple headings with no nested tags:
<h2>[^<]*</h2>
It matches the whole heading, including its tags. To select only the text between these exact opening and closing tags:
(?<=<h2>)[^<]*(?=</h2>)
For example, the second pattern selects Our services in this fragment:
<h2>Our services</h2>
Match a complete comment
This version also spans line breaks and permits ordinary angle brackets inside a comment:
<!--[\s\S]*?-->
Use Find All to inspect the matches, then make the intended edit. The lazy *? stops at the next closing delimiter rather than swallowing all comments in the file as one match.
Know the limits
The heading patterns deliberately do not handle attributes, nested elements such as <em>, or every form of HTML whitespace/capitalisation. A comment-looking string inside JavaScript is not necessarily an actual HTML comment. Template directives and old conditional comments may also be functional, so deleting all comments blindly can change behaviour.
For broad or repeatable changes, use the project's HTML/template tooling and inspect a diff. In the browser, existing trusted document content can be selected structurally without regex:
const headings = [...document.querySelectorAll('h2')]
.map(element => element.textContent);
console.log(headings);
References: Sublime Text multiple selections and DOM parsing boundaries.
Original version25 June 2013
Kept here for reference and earlier links. The updated guide above is the recommended starting point; older code may depend on retired services or different software versions.
Often you may want to change all text between certain tags within Sublime Text, remove all types of certain tags, or change all of one HTML Tag to another. This is very often the case in XML documents or if you are removing all comments.
This is easily done, thanks to the Regular Expression option within Sublime Text's find options. Use the following code, and ensuring "regular expression" icon is checked (Check the screenshot below).
This code byte will find all h2 tags within a document, from opening to closing tag, as well as content in between.
<h2>[^<>]*</h2>
This is really useful to find all HTML comments and remove them in Sublime Text!
This one will find all comments, you can then use CMD + Shift + G(Quick Find All) To highlight all of them, and delete if you wish, or change them all simultaneously.
<!--[^<>]*-->
Change it to match your needs.

Keep exploring