javascript lastindexof()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • npedia
    New Member
    • Aug 2007
    • 1

    javascript lastindexof()

    hi all.

    the enter only one word in a text box. lastindexof method




    my in input is : D:/folder name/

    i want out put is D:/folder name
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    hi ...

    js is case-sensitive ... so use:

    [CODE=javascript]'D:/folder name/'.lastIndexOf('/');[/CODE]

    to get the position of the last slash ;)

    now you have to remove the slash but you have to compare the length of your string with the 'slash-position' ... because i assume that you want to remove the ending slashes only?

    or simply use the following for the task (that uses a regEx):

    [CODE=javascript]var foo = 'D:/folder name/';
    foo = foo.replace(/\/$/, '');
    [/CODE]

    kind regards

    Comment

    Working...