PDA

Click to See Complete Forum and Search --> : regular expressions in javascript


abhijit
Jan 24th, 2003, 05:43 AM
Hi,
I have a simple doubt in client side javascript.
I have a string "ASHDHSJDHASLJDMICROADHHASJD.TIF"
I wish to find out if the last four characters in this string
are ".TIF" This could be in any case (upper case, lower case, mixed case)
If the letters are present, I want to alert("TRUE) else alert("FALSE");

How do I use a regular expression for this particular case?

Cheers!
Abhijit

DeadEyes
Jan 24th, 2003, 07:42 AM
This doesn't use a RegExp but is will do the job

function GetExt(val){
var str = new String(val);
str = str.substring(str.length -4,str.length);
return (str.toUpperCase() == '.TIF')?true:false;
}

JoshT
Jan 24th, 2003, 02:18 PM
http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/regexp.html#1193136 should help.

abhijit
Jan 25th, 2003, 12:34 AM
Thanks a lot folks.
That getExt helps me a lot. I had written something like this.


function matchDemo(str)
{
var st = new String(str);
var re = new RegExp("(\.)(tif)","i","$");
re.exec(st);
if (RegExp.$1 != '')
{
alert(RegExp.$1);
return true;
}
else
{
//alert('No Match Found');
return false;
}
}


But its not working correctly. Wondering if anyone could resolve it.

Cheers!
Abhijit

jeffmott
Jan 25th, 2003, 08:20 AM
foo.tif -- Matches. The alert displays a period
foo.tif.wav -- Matches, but shouldn't
footif -- Matches, but shouldn't

Try this instead:

return /\.tif$/i.test(str)