Sublime: replace everything between quotes - regex

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

Related

How to Match Redundant Lines From Contenteditable Div in Regex

I'm trying to process the html inside a contenteditable div. It might look like:
<div>Hi I'm Jack...</div>
<div><br></div>
<div><br></div>
<div>More text.</div> *<div><br></div>*
*<div><br></div>**<div><br></div>*
*<div><br></div>*
*<div>
<br>
</div>*
What regex expression would match all trailing <div><br></div> but not the ones sandwiched between useful divs containing text, i.e., <div> text (not html) </div>?
I have enclosed all expressions I want to match in asterisks. The asterisk are for reference only and are not part of my string.
Thanks,
Jack
You can use the pattern:
(?:<div>[\n\s]*<br>[\n\s]*<\/div>)(?!.*?<div>[^<]+<\/div>)
You can try it here.
Let me know if this works for all your cases and I will write a detailed explanation of the pattern.

Regex Pattern to Match A Href and Remove

I am trying to create a regex to match all a href links that contain my domain and I will end up removing the links. It is working fine until I run into an a href link that has another HTML tag within the tag.
Regex Statement:
(<a[^<]*coreyjansen\.com[^<]*>)([^"]*?)(<\/a>)
It matches the a href links in this statement with no problem
Need a lawyer? Contact <span style="color: #000000">Random text is great Corey is awesome</span>
It is unable to match both of the a href links this statement:
<strong><a href="http://coreyjansen.com/"><img class="alignright size-full
wp-image-12" src="http://50h0.com/wp-content/uploads/2014/06/lawyers.jpg"
alt="lawyers" width="250" height="250" /></a>
I have been trying to play with the neglected character set with no luck. If I remove the neglected character set what ends up happening is it will match two links that are right after each other such as example 2 as one match.
The issue here is that [^<]*> matches everything up until last >. That's the greedy behaviour of * asterisk. You can make it non-greedy by appending ? after asterisk(which you already do in other part of your query). It will then match everything until first occurrence of >. Then you have to change the middle part of your regex too ie. to catch everything until first tag </a> like this:
(<a[^<]*coreyjansen\.com[^<]*?>)(.*?)(<\/a>)
Use below regex which matches only a tag
(<a[^>]*coreyjansen\.com[^>]*>)
Example data
<strong><a href="http://coreyjansen.com/"><img class="alignright size-full
wp-image-12" src="http://50h0.com/wp-content/uploads/2014/06/lawyers.jpg"
alt="lawyers" width="250" height="250" /><a href="http://coreyjansen.com/"/>
Above regex will match all three a tag with your required domain.
Try above on regex
I'm playing with the following regex and it seems to be working:
<a.*coreyjansen\.com.*</a>
it captures anything between anchor tags that contain your site name. I am using javascript pattern matching from www.regexpal.com, depending on the language it could be slightly different
You need to match start of tag <a then match address before > char. You are matching wrong char. When you match that, then everithing between <a> and </a> is displayed link. I don't know why you compare to not contain quotes, every tag attribute (in HTML5) has value inside quotes, so you need to match everything except link ending tag </a>. It's done by ((?!string to not match).)* and after that should follow </a>. The result regex is:
(<a[^>]*coreyjansen\.com[^>]*>)((?!<\/a>).)*(<\/a>)

Need help in regular expression for notepad ++

Below is my sample text .
<ul>
<li>Google</li>
<li>Yahoo</li>
<li>Bing</li>
</ul>
I would like to add an extra attribute in anchor tag with the value of hyperlink like below.
<ul>
<li>Google</li>
<li>Yahoo</li>
<li>Bing</li>
</ul>
I want to do this using notepad++ regular expression. Appreciate your help !!
You can use this regular expression find/replace:
Find: >([^<>]+)</a>
Replace:  aria-label="$1"$0
Transforming Quotes
In comments you asked to also replace a single quote by a repeated single quote, in both the texts. This cannot be done in the same replace operation, but you could launch a separate one, that should be executed before the one above:
Find: '(?=[^<>]*</a>)
Replace: ''
And then after this is done, you could apply the first replace operation.
I will assume that all your tags are correctly formed (no missing closing tag, no missing bracket, etc...). You can then do something like :
Replace :
(<a[^>]*)>([^<]*)(<\/a>)
by
$1 aria-label="$2">$2$3
Demo here
Use (?<=www\.)(\w+)(\..+\")(?=>) as a find template and \1\2 aria-label="\1" as replace template.
Click on Replace All button.

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 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?