Reputation: 101
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?
Upvotes: 1
Views: 624
Reputation: 4579
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.
Upvotes: 3
Reputation: 6220
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>
Upvotes: 3
Reputation: 4523
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>
Upvotes: 3