Regex preg_replace on decimal number within a link - regex

I'd like to surround the 2.5 with a span elemtent using regular expressions preg_replace. But only in the link name, not the URL.
2.5 Subchapter
Anyone likes to help me with this challenge?
I did try this Regex validation on decimal already, but it didn't work for me.
Edit:
Thanks to ssc-hrep3's answer below, here the answer for a PHP solution (I forgot to mention it before):
$myNav = preg_replace('/(>)(\d\.\d+)/s', '$1<span>$2</span>',$myNav );
Kind Regards,
Steffano

If you have just this simple case, you could look for the > character:
(>)(\d\.\d)
And replace it with:
$1<span>$2</span>
var text = '2.5 Subchapter';
var regex = /(>)(\d\.\d)/g;
var replacement = "$1<span>$2</span>";
var result = text.replace(regex, replacement);
console.log(result);

Related

Parse string with regular expression using JavaScript

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.

RegExp to match a URL

In Google Apps Scripts, I'm trying to match a URL using RegExp using the following function.
function testRegex(){
var str = "href='https://sites.google.com/a/domain.com/image-store/images/Image1.jpg?attredirects=0'";
var regex = new RegExp('http[:a-zA-Z\.\/\-_]{0,100}Image1.jpg', 'gi');
str = str.replace(regex,"new_url");
Logger.log(str);
}
When I input the same regexp and string into the regular expression tester at http://www.regular-expressions.info/javascriptexample.html , it works. However, it doesn't work in Google Apps Scripts.
Any ideas why?
EDIT:
I figured the problem is with the underscore. Replacing with \w helps. So, when I replace the Regex with
https[\.a-zA-Z0-9\/+:\w-]{0,100}Image1.jpg
IT WORKS.
But, it still doesn't match an underscore. For example, it doesn't work with the following the URL
https://sites.google.com/a/domain.com/image-store/_/rsrc/1351707816362/images/Image1.jpg
Adding a + after the slash might do it:
function testRegex(){
var str = "href='https://sites.google.com/a/domain.com/image-store/images/Image1.jpg?attredirects=0'";
var regex = new RegExp('http[:a-zA-Z\.\/+\-_]{1,100}Image1.jpg', 'gi');
str = str.replace(regex,"new_url");
Logger.log(str);
}
I didn't debug your code, but I tried it on repl.it and verified that it is not correct in Chrome's V8 JavaScript either. I suspect there is a bug here that is unrelated to Apps Script.
EDIT: This works:
function testRegex(){
var str = "href='https://sites.google.com/a/domain.com/image-store/_/rsrc/1351707816362/images/Image1.jpg'";
var regex = new RegExp('https[\.a-zA-Z0-9\/+:\w_-]{0,100}Image1.jpg', 'gi');
str = str.replace(regex,"new_url");
Logger.log(str);
}
It didn't match underscores because you didn't specify an underscore in the character class.

AS3 Regex test for URL

I want to check if a string contains a URL.
var string:String = "Arbitrary amount of random text before and after.<br/><br/>http://www.somdomain.co.uk<br/><br/>Tel: 0123 456 789<br/>";
var pattern:RegExp = new RegExp("(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,#?^=%&:/~\+#]*[\w\-\#?^=%&/~\+#])?");
ExternalInterface.call('console.log',pattern.test(string));
This outputs false to my console, whereas when I feed the regex and string into http://gskinner.com/RegExr/, the url is found.
What am I doing wrong and how do I fix it?
I just escaped a couple extra / and it started working. Also, I generally like to use the regex literal notation to create regexes as it gives you better syntax highlighting than converting a string to a regex in the new Regex():
var pattern:RegExp = /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,#?^=%&:\/~\+#]*[\w\-\#?^=%&\/~\+#])?/;

AS3 Regular expression

Any of you gone through this task? Please tell me a solution.
I have to extract video id alone from youtube url : http://www.youtube.com/watch?v=Ls8ppLu72NQ&feature=popular
From this i need only Ls8ppLu72NQ How can i extract it. I know i can use string replace but is there a way to
extract it easily with regex.
Url can be all these formats
http://www.youtube.com/watch?v=Ls8ppLu72NQ&feature=popular
http://www.youtube.com/watch?v=Ls8ppLu72NQ
http://www.youtube.com/watch/Ls8ppLu72NQ
Try this regex:
watch(?:\/|(?:\?|.*&)v=)(\w+)
The result will be in the 1st capture group.
Demo: http://rubular.com/r/7J9FSgwBMf
Thanks a lot #Aillyn
Its worked for me.
I made it in the following way
**
var myPattern:RegExp = /watch(?:\/|(?:\?|.*&)v=)(\w+)/ig;
var str:String = "http://www.youtube.com/watch?&vx=123&v=Ls8ppLu72NT";
var result:Object = myPattern.exec(str);
trace(result[1]);
**

How do I remove "http://" from a string in actionscript?

This one may seem basic but I don't know how to do it - anybody else?
I have a string that looks like this:
private var url:String = "http://subdomain";
What regex do I need so I can do this:
url.replace(regex,"");
and wind up with this?
trace(url); // subdomain
Or is there an even better way to do it?
Try this:
url.replace("http:\/\/","");
Like bedwyr said. :)
This will match only at the beginning of the string and will catch https as well:
url.replace("^https?:\/\/","");
ActionScript does indeed support a much richer regex repetoire than bewdwyr concluded. You just need to use an actual Regexp, not a string, as the replacement parameter. :-)
var url:String;
url = "https://foo.bar.bz/asd/asdasd?asdasd.fd";
url = url.replace(/^https?:\/\//, "");
To make this perhaps even clearer
var url:String;
var pattern:RegExp = /^https?:\/\//;
url = "https://foo.bar.bz/asd/asdasd?asdasd.fd";
url = url.replace(pattern, "");
RegExp is a first class ActionScript type.
Note that you can also use the $ char for end-of-line and use ( ) to capture substrings for later reuse. Plenty of power there!