Regex expresion that should select matching text but escape everything between >< - regex

I have a lot of files where i have something similar
<span translate>textTo.translate</span>
What need to look like is
<span>{{ $t("textTo.translate") }}</span>
What i do now is find all occurrences of span translate> and replace all with span>{{ $t(" and then go thru file and finish step by step
Is there any regex that i could use in replace all to achieve this?
What i managed to do is to select all (span translate>)[^<>]+(?=<) but i cannot find replacement regex

You can use
Find: <span\s+translate>([^<>]+)<
Replace: <span>{{ $t("$1") }}<
See the regex demo.
Details:
<span\s+translate> - a <span string, one or more whitespaces and then a translate> string
([^<>]+) - Group 1 ($1 refers to this group value): one or more chars other than < and >
< - a < char
The replacement is <span>{{ $t(" + Group 1 value + ") }}< text.
See the demo:

nvm, found answer but for some reason works only in VSCode while fails in PHPStorm.
Find regex: span translate>([^<>]+(?=))<
Replace regex: span>{{ \$t("$1") }}<

Related

Regex find specific character but just when inside an HTML tag

I have an HTML string, e.g. :
<a href=“{{foo.bar}}”>some text “nice” here</a>
I'm trying to find out if any opening/closing double quote (“”, not ") is present inside an html tag (i.e. inside <>, but there could others things also in the tag).
In my example, <a href=“{{foo.bar}}”> should match but “nice” or </a> shouldn't.
What is the right regex for this ?
Actually I don't believe you've found it but you rather you fell into the common trap of regular expressions. You found a pattern which matches what you desire in a specific case.
If you place a < character inside the value of the tag of the link, <a href=“{{foo.bar}}”>some text < “nice” here</a> and your regex will match <a href=“{{foo.bar}}”> and < “nice” here</a>.
So an extra caution needs to be taken when it comes to regular expressions. To match any opening tag of html better use <\w+.*?>. After that extract whatever you find inside “”.
ok, found it : <[^>]*[“”]+[^>]*>
That does not work as you probably expect it to. When you add capturing groups, you'll see which parts of the string are actually matched by which groups:
<([^>]*)([“”]+)([^>]*)>
matches your example in this way:
<a href=“{{foo.bar}}”> a href=“{{foo.bar}} ”
^ Full match ^ 1st group ^ 2nd group ^ 3rd group (nothing)
Building on #Themelis' answer, you probably want to start with something like this:
<(\w+ [^<>“]*)“([^”]+)”([^<>]*)>
matches your example in this way:
<a href=“{{foo.bar}}”> a href= {{foo.bar}}
^ Full match ^ 1st group ^ 2nd group ^ 3rd group (nothing)

Find and replace between first '>' and next '<' in vim

I have an html table with some headers:
<th>Header01</th>
...
<th>Different Header 20</th>
I'd like to find and replace everything between the first > and the next < with {{ }}:
<td>{{ }}</td>
...
<td>{{ }}</td>
I know I can use :s/\%Vth/td/g to replace all the th with td, but how can I use Vim's regex to find and replace everything between > and < with {{ }}?
I have attempted the following without success
:s/\%V\>(.*?)\</{{ }}/g
if you would take the risk of changing a html/vim by regex, in vim, you can just do:
%s/>[^<]*</>{{ & }}</g
You may use
%s/>\(.\{-}\)</>{{ \1 }}</g
In a non-very magic mode, \< and \> are word boundaries that is why they did not work on your side. Besides, *?, a Perl-like lazy quantifier, should be written as \{-} in Vim. The % symbol at the start tells Vim to search and replace on all lines, not just the current one.
Details
> - matches a >
\(.\{-}\) - captures into Group 1 any 0 or more chars (but linebreaks, if you need to include line breaks, prepend . with \_) but as few as possible
< - matches a <
The replacement is >{{ \1 }}<, >{{, Group 1 value and }}>. g makes multiple search and replace operations on lines.
I may be misreading the question, but it appears the desired replacement text is paired double braces around 3 whitespaces (not around the original matched text: "replace everything between the first > and the next < with {{ }}". If correct, then the following simple substitute command should work:
%s/>.*</>{{ }}</g
If you have just one tag --> content per line, you can do:
:%norm cit{{ }}
As you would know, "cit" is a vim text-object that stands for "Change Inner Tag".
If you have many tags on each line you can try:
:%s,>\zs[^<]*\ze</.*,{{ }},g
For more read :h text-objects and :h \zs

Sublime text replace pattern with regex

Using Sublime Text 3 I am trying to find all instances of a <span> element where the class value is not enclosed in quotes – e.g. <span class=foo> – and I want to wrap the class value in quotes.
The following is not working as expected as a search + replace with the regex option activated:
Find what: <span class=[A-Za-z0-9]*>
Replace with: <span class="$1">
The result I am getting (which I don't want) is <span class="">
Highlighting shows that the search term is correctly matching what I want but the $1 part where I want to insert the previously captured pattern does not work. I have also tried \1 in the replace pattern.
What is wrong with my syntax?
The answer was supplied as comment. The pattern to be captured was not wrapped in brackets.
Tell it what you want to (capture): <span class=([A-Za-z0-9]*)>
Alex K.

get specific string after first occurance of string regex sublime text 2 find & replace

include_once($pathToRoot.'header.php');
echo('</div>');
assume you have variations on the above code across hundreds of files, how do you match against the first occurrence of
</div>
after
header.php'
?
In the find field:
(?s)(header\.php'.+?)</div>
In the replace (if you what to replace </div> with </test>):
$1</test>
I don't know that sublimetext2 but the regular expression would look like this:
/include_once\($pathToRoot.'header.php'\);(.*?)(<\/div>)/s
The first group would be the string between the include and the closing div and the second group would be the closing div itself.

How to write this regex expression

In my HTML I have below tags:
<img src="../images/img.jpg" alt="sometext"/>
Using regex expression I want to remove alt=""
How would I write this?
Update
Its on movable type. I have to write it a like so:(textA is replaced by textB)
regex_replace="textA","textB"
Why don't you just find 'alt=""' and replace it with ' ' ?
On Movable Type try this:
regex_replace="/alt=""/",""
http://www.movabletype.org/documentation/developer/passing-multiple-parameters-into-a-tag-modifier.html
What regex you are asking for ? Straight away remove ..
$ sed 's/alt=""//'
<img src="../images/img.jpg" alt=""/>
<img src="../images/img.jpg" />
This does not requires a regex.
The following expression matches alt="sometext"
alt=".*?"
Note that if you used alt=".*" instead, and you had <img alt="sometext src="../images/img.jpg"> then you would match the whole string alt="sometext src="../images/img.jpg" (from alt=" to the last ").
The .* means: Match as much as you can.
The .*? means: Match as little as you can.
s/ alt="[^"]*"//
This regex_replace modifier should match any IMG tag with an alt attribute and capture everything preceding the alt attribute in group #1. The matched text is then replaced with the contents of group #1, effectively stripping off the alt attribute.
regex_replace='/(<img(?:\s+(?!alt\b)\w+="[^"]*")*)\s+alt="[^"]*"/g','$1'
Is that what you're looking for?