Parse string with regular expression using JavaScript - regex

Need help with regular expression in JavaScript
I have a semicolon delimited string of hyperlinks:
"<a onclick="RemoveValue('Asthma', '1')">Asthma</a>; <a onclick="RemoveValue('Alzheimer’s Disease', '2')"> Alzheimer’s Disease</a>; <a onclick="RemoveValue('Depression', '3')">Depression</a>"
I need to remove below part of the string using regular expression:
“<a onclick="RemoveValue('Alzheimer’s Disease', '2')"> Alzheimer’s Disease</a>”
Any help would be appreciated.
Thanks,

Since I have no clue to why or what you're trying to accomplish the regex may not work. Next time, please give us some context. It helps us and you. The other thing is try to show what you've tried yourself so we don't just repeat the same suggestion you've already tried.
Anyway here's a simple regex that will do this, but technically you could just do a replace to remove the occurrences of the string you provided.
<a[^>]+onclick="RemoveValue\('Alzheimer’s Disease'[^"]+">[^<]+</a>(?:;\s)?
Regex Demo
For the JS side it should look something similar to this:
var str = "<a onclick=\"RemoveValue('Asthma', '1')\">Asthma</a>; <a onclick=\"RemoveValue('Alzheimer’s Disease', '2')\"> Alzheimer’s Disease</a>; <a onclick=\"RemoveValue('Depression', '3')\">Depression</a>";
var re = /<a[^>]+onclick="RemoveValue\('Alzheimer’s Disease'[^"]+">[^<]+<\/a>(?:;\s)?/gi;
var reOutput = str.replace(re, "");
console.log(reOutput);
Fiddle Demo - Check console window.

Related

Find multiple instances of substring in a string using Regular Expressions?

I have many lines such as this:
string s = "Some HTML with two <A HREF="links"> in one <A HREF="line">";
I need to use regular expressions to get the URLs in between the quotation marks, like this:
string all_links[] = {"links", "line"};
How would I go about doing this? Thank you.
As per your given string sample.
Use [^\HREF="]+(?=">)
Demo

Regex ignore first 12 characters from string

I'm trying to create a custom filter in Google Analytic to remove the query parts of the url which I don't want to see. The url has the following structure
[domain]/?p=899:2000:15018702722302::NO:::
I would like to create a regex which skips the first 12 characters (that is until:/?p=899:2000), and what ever is going to be after that replace it with nothing.
So I made this one: https://regex101.com/r/Xgbfqz/1 (which could be simplified to .{0,12}) , but I actually would like to skip those and only let the regex match whatever is going to be after that, so that I'll be able to tell in Google Analytics to replace it with "".
The part in the url that is always the same is
?p=[3numbers]:[0-4numbers]
Thank you
Your regular expression:
\/\?p=\d{3}\:\d{0,4}(.*)
Tested in Golang RegEx 2 and RegEx101
It search for /p=###:[optional:####] and capture the rest of the right side string.
(extra) JavaScript:
paragraf='[domain]/?p=899:2000:15018702722302::NO:::'
var regex= /\/\?p=\d{3}\:\d{0,4}(.*)/;
var match = regex.exec(paragraf);
alert('The rest of the right side of the string: ' + match[1]);
Easily use "[domain]/?p=899:2000:15018702722302::NO:::".substr(12)
You can try this:
/\?p\=\d{3}:\d{0,4}
Which matches just this: ?p=[3numbers]:[0-4numbers]
Not sure about replacing though.
https://regex101.com/r/Xgbfqz/1

Regex Python, Find Everything Inbetween Quotes after Keyword

I have strings that looks like this:
"Grand Theft Auto V (5)" border="0" src="/product_images/Gaming/Playstation4 Software/5026555416986_s.jpg" title="Grand... (the string continues for a while here)
I want to use regex to grab this: /product_images/Gaming/Playstation4 Software/5026555416986_s.jpg
Basically, everything in src="..."
At the moment I produce a list using re.findall(r'"([^"]*)"', line) and grab the appropriate one, but there's a lot of quotes in the full string and I'd like to be more efficient.
Can anyone help me put together an expression for this please?
Try with this
(?<=src=").+(?=" )
Use this as RE :
src="(.+?)"
This will return result as you want.
re.findall('src="(.+?)"', text_to_search_from)

Regular Expression for String without a "?" character to redirect to string with "?" character

On our website we occasionally experience an error where dynamic links aren't building correctly.
URLs like this
https://www.test.url.edu/collections/&edan_fq[]=p.edanmdm.indexedstructured.object_type:%22Financial+records%22&edan_fq[]=p.edanmdm.descriptivenonrepeating.record_id:item_*
Should actually be this:
https://www.test.url.edu/collections/search?edan_fq[]=p.edanmdm.indexedstructured.object_type:%22Financial+records%22&edan_fq[]=p.edanmdm.descriptivenonrepeating.record_id:item_*
We want to create a regular expression to redirect
/collections/&edan_fq[]=
to
/collections/search?edan_fq[]=
But everything after "edan_fq[]=" can change dynamically--there are thousands of permutations of the string after that point.
Does anyone know how this would be done?
If you use \& without Global Flag in Regex it will give first match. I've used JavaScript, please check this.
var data = "https://www.test.url.edu/collections/&edan_fq[]=p.edanmdm.indexedstructured.object_type:%22Financial+records%22&edan_fq[]=p.edanmdm.descriptivenonrepeating.record_id:item_*";
var regex = /\&/
data = data.replace(regex,"search?");
console.log(data);
Please check Substitution example in Regex101.

Regex coding with pattern

I have a long string url, and in it somewhere is "http://www.webcitation.org/65aOmhrdM". I need to write a regex to find "65aOmhrdM" (the pattern is always num,num,lowercase,uppercase,lowercase,lowercase,lowercase,lowercase,uppercase).
I'm looking to say:
var matches = url.match(/regex here/);
Thanks.
Here is a regex for the pattern you gave:
\d{2}[a-z][A-Z][a-z]{4}[A-Z]
If you want to pull it from that url you could do something like this:
/http:\/\/www.webcitation.org\/(\d{2}[a-z][A-Z][a-z]{4}[A-Z])/
\\d{2}[a-z][A-Z][a-z]{4}[A-Z]
Regular is:
\d\d[a-z][A-Z][a-z]{4}[A-Z]