1

I am developing an application and want to be able to grab only the resource from the URL so I have written a regular expression, but it doesn't seem to be working and I get an uncaught type error. Can anyone help with this?

var link = document.location;

var res = link.match(/\/.*php/g);
3
  • 1
    Can you show us the exact error you get? Commented Aug 12, 2014 at 15:28
  • 1
    Also, document.location isn't a string. It's a Location object. Commented Aug 12, 2014 at 15:29
  • @RocketHazmat it's in the Chrome browser "uncaught typeerror undefined is not a function" Commented Aug 12, 2014 at 15:29

2 Answers 2

5

document.location is not a string, but a Location object with properties describing the individual components of the URL. It therefore has no match method.

Try using document.location.pathname instead.

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

Comments

4

The problem is that link is not a string, so it doesn't have the match() property.

try:

var link = document.location.toString();
var res = link.match(/\/.*php/g);

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.