在jquery中匹配两个字符串

时间:2015-12-07 10:43:06

标签: javascript jquery

我有两个字符串,check_urlcurr_url

<script>
var check_url='http://localhost/project/show/{dynamic content}';
var locationHref    =   $(location).attr('href');//current url
</script>

在这里我需要匹配两个字符串。 我试过下面的代码

var urlslugRegex = '/^[a-z0-9-]+$/'; 
if(locationHref == 'http://localhost/project/show/'+(urlslugRegex)){
    alert("if");
}else{
    alert("else");
}

2 个答案:

答案 0 :(得分:2)

试试这个:

var urlslugRegex = /^http\:\/\/localhost\/project\/show\/[-a-z0-9]+$/; 
if(locationHref.match(urlslugRegex)){
   alert("if");
}else{
   alert("else");
}

注意:在字符集中,应小心放置“ - ”,因为它用作字符范围的特殊字符。最好放在字符集的开头。

答案 1 :(得分:1)

试试这个

var urlslugRegex = '([a-zA-Z0-9_-]+)'; 
if(locationHref.match('http://localhost/project/show/'+(urlslugRegex)){
    alert("if");
}else{
    alert("else");
}