Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: indexOf

  1. #1
    Regular Coder ellisd5's Avatar
    Join Date
    Jun 2002
    Location
    Uk
    Posts
    160
    Thanks
    5
    Thanked 0 Times in 0 Posts

    indexOf

    I have written a bit of code that is checking the file extension.

    I do a substring from the indexOf(".") but of course that is grabbing the first dot.

    Is there a function grans the last occurense of a dot or a Regular expression that can retrieve the file extension?

    TIA Dale
    Dale Ellis
    __________________

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    This regex: /\.([^.]+)$/ will match a fullstop, followed by capturing one or more characters that are not fullstops, followed by end of string. The result of executing /\.([^.]+)$/.exec([string FileName]) will result in a regex array [entirematch, justthefileextention]. entirematch will of course contain the fullstop while justthefileextention will not, but that's the only difference.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Regular Coder
    Join Date
    Sep 2003
    Posts
    290
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Is there a function grans the last occurense of a dot
    lastIndexOf

    Almost certainly more efficient than a regexp would be (despite how little it would matter.
    Code:
    var fn = "filename.ext";
    var ext = fn.substr( fn.lastIndexOf(".") + 1 );

  • #4
    Regular Coder ellisd5's Avatar
    Join Date
    Jun 2002
    Location
    Uk
    Posts
    160
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Ok thanks for that.
    I noticed though, if there are no "." then there is no array returned. So I guess I need to check that.

    How can I include characters that I dont want to allow, in particular at the moment, "£"

    ?
    Dale Ellis
    __________________

  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    /\.([^.£]+)$/

    Jeff Mott is probably right about the lastIndex thing, though. It is more effective than a regex, except for possibly a single character regex matching only fullstop, which you could use with the split method of a string. (Regex are compiled before command execution, so they are more effective at fixwidth searching than the string comparisons are.)
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #6
    Regular Coder ellisd5's Avatar
    Join Date
    Jun 2002
    Location
    Uk
    Posts
    160
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Ok, I think Im gonna use a bit of both.

    Im getting the extension part using lastindexOf

    I want to do a check that the whole path doesn't contain a "£" or a "@"

    How can I write a regEx to do this, I little explanation of what each bit of the regEx does would be really useful

    Thanks
    Dale
    Dale Ellis
    __________________

  • #7
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    In my sig, there are two links to an article about regex in JavaScript that I wrote three years ago. It is, if I may say if myself, the best regex reference/tutorial you will find for JavaScript. It may be of service for you, so do read it. If you still need help, you can find me here, though.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

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