Sublime text replace pattern with regex - 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.

Related

Regular expression needed to replace token and transform into a FontAwesome <i> tag

I'm looking for a regular expression to search a string for anything between the two colons, and transform it into a FontAwesome <i> tag.
What is between the ::token:: is what is exactly what inserted in the <i> FontAwesome tag
It will need to replace all of the ::tokens:: in a string as the string may contain many tokens.
Examples
Replace:
::fa-camera-retro::
with
<i class="fas fa-camera-retro"></i>
and others like:
::fb::
with
<i class="fas fb"></i>
Note:
I'm using this as a way to insert FontAwesome into Quilljs editor.
You could do something like:
"::fa-camera-retro::".replace(/::(.*)::/, (string,match) =>
`<i class="fas ${match}"></i>`
)
The replace method takes a Regular expression (/::(.*)::/). Here we match the two sets of colons, and anything in between is put into parentheses, making them a match group. The second argument to replace can be a function. The parameters are the match, and then any groups that you declare within the match. That function returns the string to replace the original whole match.
Note that this won't work for a number of matches, but you could solve that by either tweaking the regex (preferable) or by manipulating the text to manage one token at a time (slower).
Hint

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.

regex substitute two patterns in one match

I'm trying to do a find/replace in notepad++ where the string is similar to
<span class="CharOverride-1">Q</span>
With a single replace command I'd like the result to be
<span class="somethingNew">somethingElse</span>
This matches the two things I want replaced but I don't know how to form the substitution
(?<=<span class="(CharOverride-1)">)(Q)(?=<\/span>)
If possible I'd like to avoid doing something like this
(<span class=")(CharOverride-1)(">)(Q)(<\/span>)
and
\1somethingNew\3somethingElse\5
You can simlpy use 3 captures groups:
Search:
(<span class=").*?(">).*?(</span>)
Replace:
\1somethingNew\2somethingElse\3
Don't forget to check the "regular expression" checkbox.
But, if I can give you a very personal advice: don't use Notepad++...
The regular expression (?<=<span class=")CharOverride-1">Q(?=<\/span>) uses lookahead and lookbehind to find the string CharOverride-1">Q, but only where it follows the string <span class=" and is followed by </span>. Use somethingNew">somethingElse as the replacement string.

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>

Regexp: remove all tags from string except one kind of tags

I have such string
<p>test <span class=\"match\">match</span> <span class=\"testtes\">dddddd</span></p>
I want to get string without tags. But I want to save highlighting by class "match":
test <span class=\"match\">match</span> dddddd
If I want to just remove all tags I substitute all substrings that satisfied regexp /<\/?[^>]*>/ by empty string. But what regexp should I use in my special case?
UPD: The algorithm is: if you see and some sentence without tags and then then you shouldn't remove these spans; otherwise you should remove all tags
I can could do someting like this
<\/?(?![^>]*class=\\"match)[^>]*>
This would preserve the opening tag and result in this
test <span class=\"match\">match dddddd
See it here on Regexr
But how should I find the matching closing tag?
<p>test <span class=\"match\">match</span> <span class=\"testtes\">dddddd</span></p>
^^^^^^^ or the next one? ^^^^^^^
Regex can't know which closing tag belongs to the opening <span> tag that contains that class. I don't have the possibility to find matching closing tags. So its not a good idea to do this using regex.
I am quite sure the language you are using has an html parser that can be used to do this task.