Shoutbox

RegExp - exec - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: RegExp - exec (/showthread.php?tid=76932)

RegExp - exec by davidpolitis on 08-22-2007 at 10:35 AM

I need some help with RegExp, for some reason the code bellow isn't working. Can someone please help me?

The code is basically meant to turn C:\David\eg.png to eg

code:
var re = new RegExp(".*\\(.*)\\.");
var FileBase = re.exec(MyString)[1];

It's simply just plain not working at all! :S

Thanks in advance,
David.
   

RE: RegExp - exec by phalanxii on 08-22-2007 at 11:14 AM

If you want a quick fix, try:

code:
var MyString = "C:\\David\\eg.png";
var re = /([^\/\\]*)\./;
var FileBase = re.exec(MyString)[1];
But just as an explanation, when you use the new RegExp("...") notation, your regular expression is taken from what it appears as a string. That means new RegExp("\\\\a\\\\") works the same as /\\a\\/, both of which capture a string which displays as \a\. My regular expression captures a group of letters that are not slashes up to a full-stop. Be aware that if there are no matches though, your script will have an error.
RE: RegExp - exec by davidpolitis on 08-22-2007 at 11:21 AM

Mate, it won't go to the last dot after the last slash, it'll go to the first it runs into so it isn't help me much. Do you have any idea how to fix that?

Sorry if I didn't make that clear. I just gave an example, and I tried to say in my starting off sentence.

EDIT: dt helped me on IRC :P

code:
var re = /\\([^\\]*?)\.[^\.]*$/;
var FileBase = re.exec("C:\\David\\eg.png")[1];

RE: RegExp - exec by markee on 08-22-2007 at 11:29 AM

code:
if(/\\(.*)\.\w*$/.exec(MyString) !== null){
var FileBase = RegExp.$1;
}

If you are using the file using an ActiveXObject (The Scripting.FileSystemObject specifically) then you can just use .Name property.
RE: RE: RegExp - exec by davidpolitis on 08-22-2007 at 11:33 AM

quote:
Originally posted by markee
If you are using the file using an ActiveXObject (The Scripting.FileSystemObject specifically) then you can just use .Name property.

Now that you mention it... I never thought about that :(

Maybe someone else will find the code useful.

Anyway, thank you guys for all the support and help.