简体   繁体   中英

Sublime Text regex find/replace within multiple html files

I'm using Sublime Text 2 for some find&replace operations. I'm looking for a regex that replaces only the h1 classes tags to bootstrap breadcrumbs, but doesn't affect the content inside them. Each h1 tag has a different text and as there're over 700 files, doing this manually is a hard work!

For example, i'd like to replace this:

<h1 class="xyz">SOME CONTENT</h1>

To this:

<ol class="breadcrumb"> 
<li>
<a href="#">anylink</a>
</li>
<li>
<span>SOME CONTENT</span>
</li>
</ol>

That "SOME CONTENT" would be the text that I want to keep.

It is possible?

I'm using this code to find the tags and content:

<h1 class="xyz">[^<>]*</h1>

But if i replace to this:

<ol class="breadcrumb"> 
<li>
<a href="#">anylink</a>
</li>
<li>
<span>[^<>]*</span>
</li>
</ol>

It replaces the text (which I want to keep) with the expression [^<>]*.

Any idea?

You can achieve it like this :

find using :

<h1 class="xyz">([^<>]*)</h1>

replace with :

<ol class="breadcrumb"> 
<li>
<a href="#">anylink</a>
</li>
<li>
<span>$1</span>
</li>
</ol>

In the search field, you'll need to capture the text you want in a group with brackets :

<h1 class="xyz">([^<>]*)</h1>

And in the replace field, use the captured group with $1 like this :

<ol class="breadcrumb"> 
<li>
<a href="#">anylink</a>
</li>
<li>
<span>$1</span>
</li>
</ol>

Hope it helps.

Here is another example (I do not think it is necessary to include the closing > ):

<h1 class="xyz">([^<]*)</h1>

The capturing group ([^<]*) its saying “Anything that is not a < zero to more times”

And then you should use the captured group with \\1 or $1 , (I do not know how it works on sublime)

<ol class="breadcrumb"> 
   <li>
      <a href="#">anylink</a>
   </li>
   <li>
      <span>\1</span> <!-- or $1, the one sublime text uses -->
   </li>
</ol>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM