Simple TextWrangler Grep Replace - regex

I am trying to replace all h2 tags containing the class "title" with h1:
<h2 class="title">Things</h2>
I'm using the Multi-File search in TextWranger with this:
<h2 class="title">[^>]*</h2>
I'm able to find everything, but when I hit replace, it replaces my titles with the grep crap.
BEFORE: <h2 class="title">Things</h2>
AFTER: <h1 class="title">[^>]*</h1>
My problem is that the search not only replaces my tags but also replaces my content with [^>]*. I also tried this in Aptana and the same thing happened. I would appreciate some insight.

It looks like you're converting <h2 class="title"> to h1? You have to use a backreference in your "replace":
Search: <h2 class="title">([^>]*)</h2>
Replace: <h1 class="title">\1</h1> (aside- if it's a h1 do you still want to preserve the 'class="title"'?)
Note the brackets in the search regex, which save what's inside them.
Then you use \1 to pull them back out in the replace text (\1 for the first set of brackets, \2 for the second, ... )

Related

Convert all <div> to <p> using BBEdit Grep

Using BBEdit Grep, I want to convert all
<div>
Text
</div>
to
<p>
Text
</p>
The Text should, of course, remain the same, untouched.
I am using Search <div>([^>]*)</div> with Replace <p>\1</p>, but this Replace does not work when Text has tags, like <em>, or links.
Find: <(/?)div>
Replace: <\1p>
This works too:
Search: <div>([\s\S]*?)</div>
Replace: <p>\1</p>

Replace substring of a string using REGEX in Notepad++

I am using notepad++ and I want to create an automation in order to replace some strings.
In this case I am going to deal with the a href tag.
So, I will give 3 examples of some lines I have in my code :
01)
<img src="urlurlurlurl" alt="">
02)
<a href="https://url.com" class="logo"><img src="urlurlurlurl" alt="">
</a>
03)
<img src="urlurlurlurl" alt="">
04)
link
So, if I wanted to replace the full a href tag above in all 4 cases, I would use this one : <a href(.*?)a>
Now, I am trying to think of a way to replace the url within the a href tag only.
I tried using that :
href="(?s)(.*?)"|href ="(?s)(.*?)"
and it works fine because I also take into consideration that there might be a space.
But now in the replace window I have to include href=""
Is there a way to make it search for the a href tags and then replace a specific substring of it?
I want to know because there are cases where I have other tags that include a url and I want to replace it. But a generic replacement for all the strings that are included within quotes ("string") would not be good as I do not to replace all of them.
You can use a negated class to match everything before and after the href like,
(a[^>]*href\s*=\s*")[^"]*
replace with capture group $1REPLACE_STRING
Regex Demo
What it does?
a[^>]* Matches a followed by anything other than a closing >.
href\s*=\s*" Matches href=". Till here is captured in group 1.
[^"]* Matches anything other than ". This form the url that you want to replace.

How to delete a part of line that has a specific beginning and ending on Notepad++?

Let's say, for example, I have some HTML code consisting a lot of elements that looks like
<div id="1-element" class="1-element">...</div>
<div id="2-element" class="2-element">...</div>
...
<div id="99-element" class="99-element">...</div>
<div id="100-element" class="100-element">...</div>
...
I need to remove only all class="*-element" parts from the whole document but leave divs, ids and other stuff using regex in Notepad++. How am I able to do it?
\bclass="\d+-element"
This should do it for you
You can use the following regex replacement:
Find what: class="[^"]+-element"(?=[^>]*>)
Replace with: empty string
Note that [^"]+ will match anything, not just numbers before -element and (?=[^>]*>) lookahead will make sure we only remove class attribute inside a node.
This works for me:
In Notepad++, open Search windows, choose replace tab.
Find what: \sclass="\d+-element"
Check regular expression.
Click Replace All.
And the result:
<div id="1-element">...</div>
<div id="2-element">...</div>
<div id="99-element">...</div>
<div id="100-element">...</div>

Search and Replace with Regular Expression

I have the following HTML snippet and there's a bunch more divs on the page.
I'd like to surround all labels (Name, Current Position and Birth Place in this case) with strong tags. I can't use css in this case.
So I was thinking would a regular expression work in this case? More specifically, I'd like to use Visual Studio Search and Replace with Regular Expressions option to do this. So find all data to left of colon and replace value with <strong>value found</strong>
<div class="col-6">
Name:<br/>blah
</div>
<div class="col-6">
Current Position:<br/>blah
</div>
<div class="col-6">
Birth Place:<br/>blah
</div>
In the search tool, just find this:
([a-z ])+:
and replace with this:
<strong>$1</strong>:
Note: the VS search & replace is not case-sensitive by default
You then want to search for a beginning of the line (^) followed by white space (\s*) then some non-line break and non-colon ([^:\n]) followed by a colon and surround the second capture group with the <strong> tag.
Search:
^(\s*)([^:\n]+:)
Replace:
\1<strong>\2</strong>
See this fiddle for more details: http://regex101.com/r/xB8tD5/2

Regex find and replace between <div class="customclass"> and </div> tag

I cant find anywhere a working regex expression to find and replace the text between the div tags
So there is this html where i want to select everything between the <div class="info"> and </div> tag and replace it with some other texts
<div class="extraUserInfo">
<p>Hello World! This is a sample text</p>
<javascript>.......blah blah blah etc etc
</div>
and replace it with
My custom text with some codes
<tags> asdasd asdasdasdasdasd</tags>
so it would look like
<div class="extraUserInfo">
My custom text with some codes
<tags> asdasd asdasdasdasdasd</tags>
</div>
here is a refiddle that all my code is there and as you can see I want to replace the whole bunch of codes between the and tag
http://refiddle.com/1h6j
Hope you get what I mean :)
If there's no nesting, would just do a plain match non-greedy (lazy)
(?s)<div class="extraUserInfo">.*?</div>
.*? matches any amount of any character (as few as possible) to meet </div>
Used s modifier for making the dot match newlines too.
Edit: Here a Javascript-version without s modifier
/<div class="extraUserInfo">[\s\S]*?<\/div>/g
And replace with new content:
<div class="extraUserInfo">My custom...</div>
See example at regex101; Regex FAQ