sbkkoovery
sbkkoovery

Reputation: 33

match two string in jquery

I've two strings, check_url and curr_url

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

In this I need to match both strings. I tried below code

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

Upvotes: 2

Views: 693

Answers (2)

Try this :

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

Note: In a character set, "-" should be placed carefully, since it is used as a special character for character ranges. Better to put at the beginning of the character set.

Upvotes: 2

Sandeep Nambiar
Sandeep Nambiar

Reputation: 1676

try this

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

Upvotes: 1

Related Questions