Smarty replace text with double quotes - replace

I have the following string in the smarty (php templating system) variable $test:
<img height="113" width="150" alt="Sunset" src="/test.jpg"/>
I want to add "em" to the height and width like this:
{$test|replace:'" w':'em" w'|replace:'" a':'em" a'}
But this doesn't work... What's the problem and the solution?

You do know ‘em’ units in HTML width/height attributes aren't valid, right? That's CSS only.

my regex isn't the greatest, or i'd give you a better matcher, but maybe using what you have through the regex replace would work.
{$test|regex_replace:'/".w/':'em" w'|regex_replace:'/".a/':'em" a'}
other matchers to try
'/\".w/'
'/".*w/'
'/\".*w/'
i can't play with my smarty sites at the moment, but i'd first remove the " from the replacement value, to see if the bug is there, then remove it from the matcher and just look for height/width.
otherwise i'd do the replace in PHP if you can.

With Aggiorno's Smart Search and Replace you can do it like this:
Search Pattern:
<img height="$h" width="$w" $attributes/>
Replace Pattern:
<img height="$[h]em" width="$[w]em" $attributes"/>
When you click the "Search" button, all the occurrences are highlighted before applying the replacement so you can do further checking, after that you can apply the replacement confidently.

Related

Regex to delete js HTML attributes

I've got this file from google that has these js attributes like jsname="data", jscontroller="data" etc.
I'd like to use Atom's find and replace with regex feature to replace all attributes beginning with js*="*" with blanks.
How would the regex for this be?
So <div class="l-o-c-qd" jsname="name" jscontroller="somecontroller">Text</div>
will be <div class="l-o-c-qd">Text</div>
Search correct RegEx corresponding to js*="*" en replace it with nothing (check space before/after for avoid double spaces after replacement)

Sublime: replace everything between quotes

I need some help with Regular expression to Search and Replace in Sublime to do the following.
I have HTML-code with links like
href="http://www.example.com/test=123"
href="http://www.example.com/test=6546"
href="http://www.example.com/test=3214"
I want to replace them with empty links:
href=""
href=""
href=""
Please help me to create a Reg. ex. filter to match my case. I guess it would sound like "starts with Quote, following with http:// .... ends with Quote and has digitals and '=' sign", but I'm not very confident of how to write this in Reg. ex. way.
(?<=href=")[^"]*
Try this.Replace by empty string.
See demo.
https://regex101.com/r/sH8aR8/40

how to add after img tag using regex cs6

I many files of pages which has images in. I need to add a </center> after each IMG tag. I'm using dreamweaver cs6 and I got this regex so far.
find <img [^>]+> and replace $&</center>
But it doesnt work. It finds and replaces the <img> tags ok but it doesn't add the </center>
Thanks in advance.
I dont know how this are done in dreamweaver, but to keep the "found" value you should add \1 - first regexp, \2 second and so on
\1</center>
or try $1 as in htaccess, but \1 is your best bet
Try this as your regex:
(<img [^>]+>)
and this as your replace string:
$1</center>
You need to add round brackets to create a capturing group which you can then reference with $1.
NOTE: Make sure you have changed the Search field to Source Code, deselected the Ignore whitespace and Match whole word checkboxes and selected the Use regular expression checkbox in the Find and Replace dialog.

REGEX Pattern - How do I match upto a certain tag in html

I have some html which I want to grab between 2 tags. However nested tags exist in the html so looking for wouldn't work as it would return on the first nested div.
Basically I want my regex to..
Match some text literally, followed by ANY character upto another literal text string. So my question is how do I get [^<]* to continue matching until it see's the next div.
such as
<div id="test"[^<]*<div id="test2"
Example html
<div id="test" class="whatever">
<div class="wrapper">
<fieldset>Test</fieldset><div class="testclass">some info</div>
</div>
<!-- end test div--></div>
</div>
<div id="test2" class="endFind">
In general, I suspect you want to look at "greedy" vs "lazy" in your regex, assuming that's supported by your platform/language.
For example, <div[^>]*>(.*?)</div> would make $1 match all the text inside a div, but would try to keep it as small as possible. Some people call *? a "lazy star".
But it seems you're looking to find the text within a div that is before the start of the first nested div. That would be something like <div[^>]*>(.*?)<div
Read about greedy vs lazy here and check to make sure that whatever language you're using supports it.
$ php -r '$text="<div>Test<div>foo</div></div>\n"; print preg_replace("/<div[^>]*>(.*?)<div.*/", "\$1", $text);'
Test
$
Regex is not capable of parsing HTML. If this is part of an application, you're doing something wrong. If you absolutely have to parse a document, use a html/xml parser.
If you're trying to screen scrape something and don't want to bother with a parser, look for identifying marks in the page you're scraping. For example, maybe the embedded div ends just before the one you want to match, so you could match </div></div> instead.
Alternatively, here's a regex that meets your requirements. However, it is very fragile: it will break if, for example, #test's children have children, or the html isn't valid, or I missed something, etc, etc ...
/<div id="test"[^<]*(<([^ >]+).+<\/$2>[^<]*)*<\/div>/

How to Find Quotes within a Tag?

I have a string like this:
This <span class="highlight">is</span> a very "nice" day!
What should my RegEx-pattern in VB look like, to find the quotes within the tag? I want to replace it with something...
This <span class=^highlight^>is</span> a very "nice" day!
Something like <(")[^>]+> doesn't work :(
Thanks
It depends on your regex flavor, but this works for most of them:
"(?=[^<]*>)
EDIT: For anyone curious how this works. This translates into English as "Find a quote that is followed by a > before the next <".
Regexes are fundamentally bad at parsing HTML (see Can you provide some examples of why it is hard to parse XML and HTML with a regex? for why). What you need is an HTML parser. See Can you provide an example of parsing HTML with your favorite parser? for examples using a variety of parsers.
If you are using VB.net you should be able to use HTMLAgilityPack.
Try this: <span class="([^"]+?)?">
This should get your the first attribute value in a tag:
<[^">]+"(?<value>[^"]*)"[^>]*>
If your intention is to replace ALL quotation marks within tags, you could use the following regular expression:
(<[^>"]*)(")([^>]*>)
That will isolate the substrings before and after your quotation mark. Note that this does not attempt to match opening and closing quotation marks. It simply matches a quotation mark within a tag.