-4

i have a link that is

dashboard/permissions?page=2

so in javascript i want check if this link's last has "?page=2" or not

if has then remove it

Note: here ?page=2 2 is changeable

so how to do it in javascript

Shojib Flamon
  • 1,237
  • 3
  • 12
  • 18

2 Answers2

0
var testStr = document.URL;
console.log(testStr.match(/\?page=\d+/) !== null);
charles
  • 547
  • 1
  • 3
  • 11
  • @SuperScript While this answer is a bit bare in terms of explanation, it is still valid. – keeehlan Mar 16 '15 at 20:41
  • @shruggernaut: Tiny code snippets are better as comments. It looks like the right code; explain a little on what it does and you'll be good. – Robbie Wxyz Mar 16 '15 at 20:43
  • @SuperScript They're better as comments when they are intended as a comment, perhaps on somebody else's answer, but if this were posted as a comment on the question, you could rest assured that somebody might come along and request that they post it as an answer. – keeehlan Mar 16 '15 at 20:45
  • @shruggernaut: while they work, code-only answers are not likely to be useful to future users. [See](http://meta.stackoverflow.com/q/258673/2619939) [these](http://meta.stackexchange.com/q/95470/241819) [meta](http://meta.stackexchange.com/q/146865/241819) posts. – Robbie Wxyz Mar 16 '15 at 20:54
0

Try the following regex: \?page=\d+:

var url = 'dashboard/permissions?page=2';
alert(url.match(/\?page=\d+/));

And to remove it :

if(url.match(/\?page=\d+/)!==null) {
    url=url.replace(/\?page=\d+/, "");
    alert(url);
}
//returns "dashboard/permissions"
cнŝdk
  • 31,391
  • 7
  • 56
  • 78