0

How to check if a url or url pattern is presnt or not in a string using javascript.

<script language="javascript">
var str="http://localhost/testprj?test=123213123";
var s=location.href;
if(s.match('/http://localhost/testprj?test=1232/'){
    alert('welcome!!');
}
</script>

what i need is to check the url pattern.

complete code

<html>
<head>
</head>
    <body>
<style>
.active{
    color:#009900;
    font-weight:bold;
}
</style>

    <div id="menu">
    <ul><li>
    <ul><li>    <a href="html1.html">0</a>
        <a href="html.html" >1</a>
        <a href="2">2</a>
        </li>
    </ul></li>
    </ul>
    </div>

    <script language="javascript">

        var c=document.getElementById('menu').getElementsByTagName('a').length;
        var v=document.getElementById('menu').getElementsByTagName('a');
        var s=location.href;
        //alert(s.match('html'));
        for(var i=0;i<c;i++){
            //alert('href'+v[i].className);
            if(v[i].href==location.href){
                v[i].className='active';
            }

        }

    </script>   


    </body>
</html>

this is working fine , but if the get params are cause some problms...

like page.php?page=userlist works fine

but

like page.php?page=userlist&id=121221

this is the base link url link

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
coderex
  • 27,225
  • 45
  • 116
  • 170
  • In the complete code, I don't see any URL regex (along the lines of what we have been suggesting). However, if I understand "get params are cause some problms" right, you may need to do some server-side (PHP) escaping of the regex. – Matthew Flaschen Jul 07 '09 at 08:46

5 Answers5

4

For pattern checking, you will want to look into regular expressions.

What particular pattern do you want to check for? If you just want to check whether a string is a URL, the following code should do:

var myString = "http://blah.com/helloworld/";
if (myString.match(/http:\/\//)) {
    alert("'myString' is a URL.");
}

Steve

Steve Harrison
  • 121,227
  • 16
  • 87
  • 72
2
/http:\/\/localhost\/testprj\?test=1232/.test(s)
Joe Chung
  • 11,955
  • 1
  • 24
  • 33
0

I think you just had some minor syntax issues:

var re = /http:\/\/localhost\/testprj\?test=1232/
var s=location.href;
if(s.match(re)) { 
  alert('welcome!!'); 
} 
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • /http:\/\/localhost\/testprj\?test=1232/ i cant use like this. because , please check my Question i will update – coderex Jul 07 '09 at 08:17
0

note that regular expressions are a data type of their own in javascript: no quotes or anything, just / delimeters

jrharshath
  • 25,975
  • 33
  • 97
  • 127
-1

Yes finaly i got the solution i used the functions

for(var i=0;i<len;i++){
        if(strstr(url,a[i].href)) {
            a[i].className='active';
        }
    }

    function strstr( url, href) {
        var pos = 0;
        url += '';
        pos = url.indexOf( href );
        if (pos == -1) {
            return false;
        } 
        else{
            if(strcmp(url,href)==0)
               return 1;
       }

    }
    function strcmp ( str1, str2 ) {
       return ( ( str1 == str2 ) ? 0 : ( ( str1 > str2 ) ? 1 : -1 ) );
    }

like this way, its working fine!!!

Thank you

coderex
  • 27,225
  • 45
  • 116
  • 170
  • Althoug it looks ok, the use of regular expressions is the correct answer to the question "checking a pattern present in a string using javascript" since this function only can check for substrings occurring in the url. For the specific case of substring checking a good question and answer has already been posted: http://stackoverflow.com/questions/1789945/how-can-i-check-if-one-string-contains-another-substring – atfornes Feb 22 '16 at 18:44