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
Related
When I woke up this morning, I didn’t know a stroke of regex. By the time I went to Mass, I’d been able to cobble together this regex to find occurrences of ‘Mph’ in an html document.
(?i)(?<=[\s|\d])mph+
If I run it against the following test data:
<div class="vsMph">
<p>95 Mph</p>
</div>
<div class="vsMph">
<p>95Mph</p>
</div>
It correctly matches:
‘ Mph’ and
‘Mph’
And equally correctly leaves the ‘vsMph’ alone, which is exactly what I want. Eventually, I'm going to use the same technique to match knots, ft, in, km and so on.
I’m executing this expression in in Sublime Text 3 using RegReplace and ultimately, what I hope to do is to use this regular expression to find all occurrences of ‘Mph’ preceded by a space or a digit and:
Enclose ‘Mph’ in <abbr> tags.
Add a space between the digit and the
opening <abbr> tag if there was no space between the last digit and
'Mph' originally.
In other words, I want to convert the above test data to:
<div class="vsMph">
<p>95 <abbr title="Miles per hour">Mph</abbr></p>
</div>
<div class="vsMph">
<p>95 <abbr title="Miles per hour">Mph</abbr></p>
</div>
I can get RegReplace to add the <abbr> tags as described in 1. above, but I’ve searched around on Google and I can’t find anything that tells me how to conditionally insert a space in a regex replace.
So I’m wondering. Is it possible in the first place to conditionally add a space in a regex replacement and if so how do I do it, or do I have to search for ‘\sMph’ and ‘\dMph’ and replace them separately?
Regards.
I would suggest using groups to match Mph. You could search for simply the following regex:
(\d)(\s)?(Mph)
Then replace using groups
$1 <abbr title="Miles per hour">$3</abbr>
output:
<div class="vsMph">
<p>95 <abbr title="Miles per hour">Mph</abbr></p>
</div>
<div class="vsMph">
<p>95 <abbr title="Miles per hour">Mph</abbr></p>
</div>
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 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, ... )
Could use some help with Regex searching with NetBeans 7.01's find function.
I'm trying to exclude multiple strings. Specifically, the target lines:
<div class="table_left">
<div class="table_right">
<div class="table_clear">
I need to match only the third and other Div classes that are not either table_left or table_right.
I've tried:
class="table_(((?!left).*)|((?!right).*))
and
class="table_(left|right){0}
I realized while pasting my first Regex line that I'm matching not right OR not left, which is returning both. What is the proper way to specify two conditions? The and operator?
The joys of searching for words that are also Boolean operators...
Try this pattern:
<div\s+class="(?!table_(left|right))[^"]+"
which wouldn't match:
<div class="table_left">
<div class="table_right">
but would match:
<div class="table_clear">
<div class="foo">
EDIT
The HT wrote:
I need to match only classes that begin with table, but are not right or left
Ah, okay, that would look like:
<div\s+class="table_(?!left|right)[^"]+"
or
<div\s+class="table(?!_left|_right)[^"]+"
as you already found yourself (but I included it in my answer for completeness sake).
A quick explanation of the pattern <div\s+class="table_(?!left|right)[^"]+":
<div # match '<div'
\s+ # match one ore more space chars
class="table_(?!left|right) # match 'class="table_' only if it is not followed by 'left' or 'right'
[^"]+ # match one or more characters other than '"'
" # match a '"'