How to compare URL with any number?

时间:2015-10-20 16:20:52

标签: javascript

I'm trying to compare URL with javascript the current url is:

http://domain/products?save&step&id=287

I used window.location.search, it rendered:

?save&step&id=287

I need to know how to compare URL with any number like this pseudocode, I used (any_number) as example of random number, it doesn't need to match equal number since I had no idea which is properly method.

 var prod_url = window.location.search;

 if(prod_url == "?save&step&id="+(any_number){    
  alert("URL is matched");
 }

I'm open with all suggestions, any method with excepted result is welcome.

3 个答案:

答案 0 :(得分:1)

要将字符串与正则表达式进行比较,您必须使用match()函数。

匹配"任何数字"的示例将是:

myString.match(/[0-9]+/); 

答案 1 :(得分:1)

您可以使用正则表达式:

所以你的if语句应该是这样的:

if ((/\\?save&step&id=\d/).test(prod_url)) {    
  alert("URL is matched");
}

这应该适用于任何数字。

答案 2 :(得分:1)

请试试这个:

var prod_url = "?save&step&id=12345";
if (/\?save\&step\&id=[0-9]+$/.test(prod_url)) {
  alert("URL is matched");
}