user3723666
user3723666

Reputation: 231

Regular Expression in JavaScript giving an uncaught type error

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);

Upvotes: 1

Views: 99

Answers (2)

sejordan
sejordan

Reputation: 321

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);

Upvotes: 4

Alnitak
Alnitak

Reputation: 339786

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.

Upvotes: 5

Related Questions