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 8490341
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:05:13+00:00 2026-06-10T22:05:13+00:00

I have this url in javascript /people/things/ I want a function that will only

  • 0

I have this url in javascript “/people/things/” I want a function that will only return “things” how do I do this?

I’ve tried ugly stuff. Like iterating but I get stuck when I have to figure out how to delete the rest of the string and its very slow asymptotically.

I keep getting “ILLEGAL”. Super annoying.

$(function() { 

    var parse = function(str) {

        var a = "";   
        if (str.length && str.length > 1) {

           str = str.slice(0, str.length -1);   //remove first

            for (var i = str.length; i > 1; i--) {
                if (str[i] !== '\\' ) {
                      a += str[i];
                    }

            }


        }

        return a;

    };   

       c = "\things\stuff\";
       alert(parse(c));        //result should be "things"                

});​
  • 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-10T22:05:15+00:00Added an answer on June 10, 2026 at 10:05 pm

    First of all, you could simplify your code using match() instead:

    var s = "/people/things/";
    var match = s.match(/\/([^/]+)\/$/);
    var things = match[1]; // "things"
    

    With respect to the ILLEGAL error, that’s because you forgot to escape the \ in your string. So instead of \things\stuff\, use \\things\\stuff\\.

    Now, to your parse function. The general idea is correct, but you have a couple of implementation errors:

    1. break out of the loop as soon as you find \\ (the else part of your if); otherwise, you’ll just skip the \, adding all other characters;
    2. You are traversing the array from back to start, and adding each character to a string; thus, in the end, you’ll get a reversed string. To fix it, instead of using a += str[i], use a = str[i] + a.
    3. i should start with a.length - 1, not a.length.

    Here’s the final version:

    var parse = function(str) {
      var a = '';   
      if (str.length && str.length > 1) {
        str = str.slice(0, str.length -1);
        for (var i = str.length - 1; i > 1; i--) {
          if (str[i] !== '\\' ) {
            a = str[i] + a;
          } else {
            break;   
          }
        }
      }
      return a;
    };
    

    DEMO.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this URL example.com/photo?people I want to rewrite this URL in the form
I have this generic javascript function to open a window: function OpenWindow(url,windowname,wide,high) { spop=window.open(url,windowname,width=+wide+,height=+high+,scrollbars=1,resizable=1,statusbar=1,menubar=0);
Sorry, but I'm new to accessing return from another domain. I have this url
I have a url which looks like this https://test.high.com/people/11111111-name-firstname-_custa/deals/new Now i need to match
Let's say for example, I have the following javascript function that returns a boolean:
Let's say I have a URL: http://example.com/person/list This website will display a list of
I have created an iframe code that I want people to use as a
Hi have this URL string which I need to extract possibly using regex but
Let's say I have this url: www.example.com/test.php?page=4 How do i retrieve the value of
Say I have this url: http://site.example/dir/ In this folder I have these files: test.ascx.cs

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.