Avatar of lulu50
lulu50
Flag for United States of America asked on

substring a path to return dir name

Hi,

I need your help!!

My javascript:
 var URLname2 = document.location.href.match(/.*\/(.*)$/)[0];

the variable "URLname2" generate this path below
"http://localhost:00000/BusinessRules/Rule/1"

what I want for the "var URLname2" is to pull just the word "Rule" from the whole path

I'm not sure how this is done, but I tried Substring and didn't work. 

Thank you so much for your help, 
Lulu50

Open in new window

ASP.NETJavaScriptjQueryAJAX

Avatar of undefined
Last Comment
Juan Carlos

8/22/2022 - Mon
leakim971

could you rephrase what you need?
leakim971

maybe :
var URLname2 = "http://localhost:00000/BusinessRules/Rule/1";
URLname2 = URLname2.replace(/Rule\//gi,"")

Open in new window


Capture.JPG
lulu50

ASKER
Hi leakim971,

from this path;

"http://localhost:00000/BusinessRules/Rule/1"

I want to store the word "Rule" in a variable

var dir = "Rule";
Your help has saved me hundreds of hours of internet surfing.
fblack61
lulu50

ASKER
I need this result

URLname2 = "Rule";
lulu50

ASKER
I want to remove "http://localhost:00000/BusinessRules/
and
I want to remove "/1"
from this path: "http://localhost:00000/BusinessRules/Rule/1"
and
store "Rule" in my variable URLname2
lulu50

ASKER
I got it

Thank you

            var pathArray = window.location.pathname.split('/');
            var secondLevelLocation = pathArray[2];
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Juan Carlos

var URLname2 = document.location.href.match(/.*\/(.*)$/)[0];
var rule = URLname2.split('/')[4];

Open in new window

ASKER CERTIFIED SOLUTION
lulu50

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
lulu50

ASKER
Thank you
Juan Carlos

I'm late! :(
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy