Results 1 to 5 of 5

Thread: regular expressions in javascript

  1. #1

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Lightbulb regular expressions in javascript

    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
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  2. #2
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196
    This doesn't use a RegExp but is will do the job
    Code:
    function GetExt(val){
    	var str = new String(val);
    	str = str.substring(str.length -4,str.length);
    	return (str.toUpperCase() == '.TIF')?true:false;
    }

  3. #3
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    Josh
    Get these: Mozilla Opera OpenBSD
    I have books for sale: "MCSD in a Nutshell" and "VB Distributed Exam Cram" - PM me for details. Will also trade for a decent ATX Pentium 2 MB/CPU/RAM combo.

  4. #4

    Thread Starter
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228
    Thanks a lot folks.
    That getExt helps me a lot. I had written something like this.

    PHP Code:
        function matchDemo(str)
        {
            var 
    st = new String(str);
            var 
    re = new RegExp("(\.)(tif)","i","$");
            
    re.exec(st);
            if (
    RegExp.$!= '')
            {
                
    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
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  5. #5
    Member
    Join Date
    Jan 2003
    Posts
    44
    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)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width