Let's Create Something Extraordinary!

WebGate is not just a design studio; we are your partners in digital transformation. Whether you're launching a new web application, revitalizing your brand, or adapting to the mobile era, we have the expertise to bring your vision to life. Ready to elevate your online presence?

Share on Facebook Share on Facebook icon Share on Twitter Twitter share icon

Notepad++, Sublime Text Editor Replacement with Regular Expressions

Notepad++, Sublime Text Editor Replacement with Regular Expressions thumb

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>
  1. Open the "Find and Replace" tool: Press Ctrl+H in Windows/Linux or Cmd+Opt+F on macOS.
  2. Enable regular expressions: Click the .* button in the bottom left corner of the "Find and Replace" dialog to enable regex mode.
  3. Enter the regex in the "Find what" field: Use this regex to precisely match any attributes inside <h1> tags:
(<h1)[^>]*(>)
  1. 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.
  2. Set the replacement text: In the "Replace with" field, use:
$1$2
  1. This replacement string uses $1 and $2 to refer to the captured groups from the regex, effectively restoring the tag without its attributes.
  2. 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.

You might also like

Consolidating Multiple Domains into a Single Domain
jQuery Event for Radio Checked + img src Replace