1

How to extract a string from the extractd url using js or jquery?

2
  • 1
    String.substr - w3schools.com/jsref/jsref_substr.asp Commented Apr 28, 2011 at 5:30
  • Are you trying to extract a certain section, or a certain substring/pattern? Commented Apr 28, 2011 at 5:33

2 Answers 2

1

With substring function, which gets the caracters between the indexes supplied For example, if you are navigating in http://stackoverflow.com/questions,

alert(location.href.substring(7, 20))

will show "stackoverflow"

I saw you asked a similar question before, if you explain why you need to do this, we could help you better

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

2 Comments

say the url is http:localhost:8080/Sample/ex1/sample1.jsp?cuId=U113&pid=123, now i have to captute the cuId and pid. .
@jagrti: I think, my answer is working solution. Maybe it should be accepted.
0

You can use Regular Expression to parse URL - see http://www.javascriptkit.com/jsref/regexp.shtml

var re = /(http.?):\/\/([^\/]*)\/(.*)/
var url = 'http://stackoverflow.com/questions/5814008/qts-on-javascript-jquery';
var tokens = url.match(re);
var theWholeUrl = tokens[0];
var protocol = tokens[1]
var domain = tokens[2];
var partAfterDomain = tokens[3];

alert('theWholeUrl: ' + theWholeUrl);
alert('protocol: ' + protocol)
alert('domain: ' + domain);
alert('partAfterDomain: ' + partAfterDomain);

Another example based on your comment:

var url = 'http://localhost:8080/Sample/ex1/sample1.jsp?cuId=U113&pid=123&anotherKey=anotherValue'
var re = /.+?cuId=(.*)&pid=([^&]*)/;
var tokens = url.match(re);
var cuId = tokens[1];
var pid = tokens[2];
alert('cuId: ' + cuId);
alert('pid: ' + pid);

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.