Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8428501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:04:31+00:00 2026-06-10T05:04:31+00:00

The Question: How do you determine a variable length string inbetween two similar characters?

  • 0

The Question: How do you determine a variable length string inbetween two similar characters?

Allow me to clarify:

take the two URLs:

/designs/2012/mobile/v4/~html/ecom/

/designs/2012/mobile/v4/~html/legal/

I’m trying to build an anchor with an ID that equals the name of each line’s current directory: ecom, legal, local, etc.

So the above directories would result in a page with the following:

<a href="/designs/2012/mobile/v4/~html/ecom/" id="ecom">/designs/2012/mobile/v4/~html/ecom/</a>
<a href="/designs/2012/mobile/v4/~html/legal/" id="legal">/designs/2012/mobile/v4/~html/legal/</a>

I’ve been playing around with .lastIndexOf but am having little luck. I figured if I could say (warning psuedo-code ahead!) “get the 2nd to last / and pull the string inbetween that and the last /” but I’m not quite sure how to do that.

Any tips or pointers?

Here’s my code thus far:

$(document).ready(function() {
$.ajax({
    url: "./~html",
    success: function(data){
        $(data).find("a").each(function(){
            var path = $(this).attr("href");
            var fileNameIndex = path.lastIndexOf("/") + 1;
            var dirNameIndex = path.lastIndexOf("/");
            var dirNameLength = path.length -1;
            var file = path.substr(fileNameIndex);
            var directory = path.substr(dirNameIndex);
            console.log("length of string: " + dirNameLength);
            console.log("Directory Index: " + dirNameIndex);
            console.log(directory);
            var idIndex = 1;

            if (file != "") {
                $('<li></li>').html('<a href=\"' + path + '\">' + file + '</a>').appendTo('#files');
            } else {
                $('<li></li>').html('<a href=\"' + path + '\"' + 'id=\"' + directory + '\">' + path + '</a>').appendTo('#directories');
            }

        });
    }
});

});

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:04:32+00:00Added an answer on June 10, 2026 at 5:04 am

    One possible way is by splitting the string by slash into an array:

    var path = '/designs/2012/mobile/v4/~html/ecom/';
    var components = path.split('/');
    var last_part = components[components.length-2];
    alert(last_part); // alerts 'ecom'
    

    path.split('/') will give you an array of 8 items (components.length is 8):

    components[0] = '';
    components[1] = 'designs';
    components[2] = '2012';
    components[3] = 'mobile';
    components[4] = 'v4';    
    components[5] = '~html';
    components[6] = 'ecom';
    components[7] = '';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The question is related to financial products, interest rates and two properties that determine
if i have a variable from fgets for example fgets(question,200,stdin); how do i determine
Question: How can I determine all processes in the child's Process Tree to kill
My Question Is: What's a fast way to determine whether a number is contained
Dumb question.. what logic does the database use to determine the result set if
The question is rather straightforward but not answered by searching. How do I determine
Another RegEx question for a search I'm trying to do. The Problem : Determine
I can see this question (or a similar one) has been asked a few
I have a question in my todays Exam in which I have to determine
Feel free to question my sanity. I need to determine if an Action<T> vs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.