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>
Related
I've been trying for hours now.
I need to get the linkable text meaning, all text from a webpage source that is between <a href> and </a> except the other tags that are nested between the <a> tags.
Example:
<a href="blabla.net">THIS TEXT
<img src="hhh.jpg" /> THIS TEXT TOO
<span> ALSO THIS TEXT. </span>AND ALSO THIS TEXT</a>
You could use a simple regular expression with a non greedy group:
<[aA]\b[^\>]*>([\w\W]*?)<\/[aA]>
You can test it on this page by hitting F12 then typing
$(document.body).html().match(/<a\b[^\>]*>([\w\W]*?)<\/a>/ig)
You can try the following Regular expression, that returns the text between tags in four groups:
(?<=>)[^<]+?(?=<)
It removes tags from the text.
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>
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
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
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, ... )