0

I have a URL like

server/area/controller/action/4/?param=2"

in which the server can be

http://localhost/abc
https://test.abc.com
https://abc.om

I want to get the first character after "action/" which is 4 in the above URL, with a regex. Is it possible with regex in js, or is there any way?

3
  • I believe first parameter is param=2 and not 4?? Commented Jul 13, 2012 at 12:29
  • not actually this is mvc url so first parameter is 4. Commented Jul 13, 2012 at 12:32
  • i just want to get 4 from this string, let skip discussion about parameter Commented Jul 13, 2012 at 12:32

3 Answers 3

3

Use regex \d+(?=\/\?)

var url = "server/area/controller/action/4/?param=2"; 
var param = url.match(/\d+(?=\/\?)/);

Test code here.

Sign up to request clarification or add additional context in comments.

1 Comment

If the first parameter may contain also non-digit characters, then use regex [^\/]+(?=\/\?)
1

Using this regex in JavaScript:

action/(.)

Allows you to access the first matching group, which will contain the first character after action/ -- see the examples at JSFiddle

Comments

1

This way splits the URL on the / characters and extracts the last but one element

var url = "server/area/controller/action/4/?param=2".split ('/').slice (-2,-1)[0]; 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.