Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search and replace outer tag in Atom using REGEX

Using Atom, I'm trying to replace the outer tag structure for multiple different texts within a document. Also using REGEX, which I'm not versed enough to come up with my own solution

HTML to be searched <span class="klass">Any text string</span>

Replace it with <code>Any text string</code>

My REGEX (<?span class="klass">)+[\w]+(<?/span>)

Is there a wildcard to "keep" the [\w] part into the replaced result?

like image 811
Sergio Majluf Avatar asked Feb 01 '26 12:02

Sergio Majluf


1 Answers

You can use a capture group to capture the text in between the <span> tags during the match, and then use it to build the <code> output you want. Try the following find and replace:

Find:

<span class="klass">(.*?)</span>

Replace:

<code>$1</code>

Here $1 represents the quantity (.*?) which we captured in the search. One other point, we use .*? when capturing between tags as opposed to just .*. The former .*? is a "lazy" or tempered dot. This tells the engine to stop matching upon hitting the first closing </span> tag. Without this, the match would be greedy and would consume as much as possible, ending only with the final </span> tag in your text.

like image 65
Tim Biegeleisen Avatar answered Feb 04 '26 05:02

Tim Biegeleisen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!