Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you determine the current directory in a path string?
    primarykey
    data
    text
    <p><strong>The Question: How do you determine a variable length string inbetween two similar characters?</strong></p> <p>Allow me to clarify:</p> <p>take the two URLs:</p> <p><strong>/designs/2012/mobile/v4/~html/ecom/</strong></p> <p><strong>/designs/2012/mobile/v4/~html/legal/</strong></p> <p>I'm trying to build an anchor with an ID that equals the name of each line's current directory: ecom, legal, local, etc. </p> <p>So the above directories would result in a page with the following:</p> <pre><code>&lt;a href="/designs/2012/mobile/v4/~html/ecom/" id="ecom"&gt;/designs/2012/mobile/v4/~html/ecom/&lt;/a&gt; &lt;a href="/designs/2012/mobile/v4/~html/legal/" id="legal"&gt;/designs/2012/mobile/v4/~html/legal/&lt;/a&gt; </code></pre> <p>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.</p> <p>Any tips or pointers?</p> <p>Here's my code thus far:</p> <pre><code>$(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 != "") { $('&lt;li&gt;&lt;/li&gt;').html('&lt;a href=\"' + path + '\"&gt;' + file + '&lt;/a&gt;').appendTo('#files'); } else { $('&lt;li&gt;&lt;/li&gt;').html('&lt;a href=\"' + path + '\"' + 'id=\"' + directory + '\"&gt;' + path + '&lt;/a&gt;').appendTo('#directories'); } }); } }); </code></pre> <p>});</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload