-2

Im trying to remove the 'page=?' (? = only numbers) from URL path with following method:

var urlslugRegex = '/^[a-z0-9-]+$/';
var url = window.location.href;
var lastpath = url.substring(url.lastIndexOf('/') + 1);
var lasturl = lastpath.replace('page=' + urlslugRegex, '');

but the regular expression not working. Any ideas?

idmean
  • 14,540
  • 9
  • 54
  • 83
zagzter
  • 229
  • 1
  • 11

1 Answers1

0
var urlslugRegex = '/^[a-z0-9-]+$/';
var url = window.location.href;
var lastpath = url.substring(url.lastIndexOf('/') + 1);
var lasturl = lastpath.replace('page=' + urlslugRegex, '');

The regex which is being sent to replace() function is

page=/^[a-z0-9-]+$/

I hope, this is not what you wanted.

You can fix it by simply by

var lasturl = lastpath.replace(/^page=[a-z0-9-]+$/, '');

Rahul Gandhi
  • 1,035
  • 1
  • 11
  • 24