How to search a particular substring in square bracket

Discussion in 'Javascript' started by balakrishnan.dinesh@gmail.com, Dec 6, 2006.

  1. Guest

    Hi frnds,

    Im having a problem in searching out a particular substring in a
    string.

    Here the code:
    ---------------------
    <html>
    <script>
    var tmpString="[Wed May 3 18:25:14 2006] [192.168.1.61] [GET
    /manual/en/images/down.gif] [304] [0] [] [396 msecs]";
    var ip="[192.168.1.61]";
    var arr=tmpString.match(ip)
    (or)
    var arr=tmpString.search(ip)

    alert(arr);
    </script>
    <body>
    </body>
    </html>

    So in my tmpString variable, i have the data like this with square
    brackets,Now i want to compare the ip variable value with tmpString
    contained ip value, due to this sqaure brackets im not able to compare
    it accurately.I used "match" and "search" commands, both are not
    useful. While using "search" , it is showing error if square bracket
    used.using match it is returning some interger.
    If im comparing with square brackets, then some other problems are
    creating i.e, if i give 192.168.1.6 as ip variable value and compared
    with tmpString value, then also it is showing output as the correct
    one.

    Can anyone give me a proper solution for this

    Thanks
    Dinesh....
    , Dec 6, 2006
    #1
    1. Advertising

  2. Noah Sussman Guest

    wrote:
    > Im having a problem in searching out a particular substring in a
    > string.
    > var tmpString="[Wed May 3 18:25:14 2006] [192.168.1.61] [GET
    > /manual/en/images/down.gif] [304] [0] [] [396 msecs]";
    > var ip="[192.168.1.61]";
    > var arr=tmpString.match(ip)
    > (or)
    > var arr=tmpString.search(ip)
    >
    > So in my tmpString variable, i have the data like this with square
    > brackets, Now i want to compare the ip variable value with tmpString
    > contained ip value, due to this sqaure brackets im not able to compare
    > it accurately.


    Both match and search take regular expressions as their arguments.
    And, if given a non-regex argument, match() and search() automatically
    _convert_ that argument to a regex.

    Square brackets [] are a regular expression operator. In a regex,
    "[192.168.1.61]" matches _one_character,_ as long as that character is
    _any_ of 1,9,2,6 or 8.

    > While using "search" , it is showing error if square bracket
    > used.


    Hmm, when I run your example code using search(), arr evaluates to 11.
    11 is the position in tmpString of the first match for the regex
    "[192.168.1.61]"; which is the digit "1" in "18:25:14".

    > using match it is returning some interger.


    Agreed. Using match() in your example, arr evaluates to 1. In this
    case, match() is returning the string that was matched. But the regex
    "[192.168.1.61]" only matches one character, the 1 in "18:25:14".

    If you escape each of the square brackets in ip with a double
    backslash, you should get more reasonable results:

    var ip="\\[192.168.1.61\\]";
    var tmpString="[Wed May 3 18:25:14 2006] [192.168.1.61]
    [GET/manual/en/images/down.gif] [304] [0] [] [396 msecs]";
    var matched=tmpString.match(ip);
    var searched=tmpString.search(ip);

    Now matched should contain "[192.168.1.61]", which is the part of
    tmpString that matches ip. And searched should contain "26", which is
    the position in tmpString of the first character of the substring that
    matches ip.
    Noah Sussman, Dec 6, 2006
    #2
    1. Advertising

  3. RobG Guest

    wrote:
    > Hi frnds,
    >
    > Im having a problem in searching out a particular substring in a
    > string.
    >
    > Here the code:
    > ---------------------
    > <html>
    > <script>
    > var tmpString="[Wed May 3 18:25:14 2006] [192.168.1.61] [GET
    > /manual/en/images/down.gif] [304] [0] [] [396 msecs]";
    > var ip="[192.168.1.61]";
    > var arr=tmpString.match(ip)


    The paramter supplied to match is supposed to be a regular expression,
    not a string. If you just want to see if tmpString contains the string
    "[192.168.1.61]" then use test (tmpString wrapped for posting):

    var tmpString = "[Wed May 3 18:25:14 2006] [192.168.1.61]"
    + " [GET /manual/en/images/down.gif] [304]"
    + " [0] [] [396 msecs]";
    alert( /\[192\.168\.1\.61\]/.test(tmpString) );

    If you wish to supply the IP address as a variable, you can use:

    var ip = "192.168.1.61";
    var re = new RegExp('\\['+ip.replace(/\./g,'\\.')+'\\]');
    var tmpString = "[Wed May 3 18:25:14 2006] [192.168.1.61]"
    + " [GET /manual/en/images/down.gif] [304]"
    + " [0] [] [396 msecs]";
    alert( re.test(tmpString) );

    A regular expression of:

    var re = new RegExp('\\[' + ip + '\\]');

    may be sufficient, but there is a good chance it many not. Please test
    thoroughly.

    --
    Rob
    RobG, Dec 6, 2006
    #3
  4. VK Guest

    wrote:
    > <html>
    > <script>
    > var tmpString="[Wed May 3 18:25:14 2006] [192.168.1.61] [GET
    > /manual/en/images/down.gif] [304] [0] [] [396 msecs]";
    > var ip="[192.168.1.61]";
    > var arr=tmpString.match(ip)
    > (or)
    > var arr=tmpString.search(ip)
    >
    > alert(arr);
    > </script>
    > <body>
    > </body>
    > </html>


    if (tmpString.indexOf(ip) != -1) {
    // match found
    }
    else {
    // no match found
    }

    KISS rules :)
    VK, Dec 6, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    1,106
    Victor Bazarov
    May 4, 2007
  2. Wabiloo
    Replies:
    1
    Views:
    819
    Martin Honnen
    Jan 25, 2008
  3. Replies:
    1
    Views:
    2,212
    Pavel Lepin
    Mar 14, 2008
  4. winterTTr
    Replies:
    2
    Views:
    345
    winterTTr
    Apr 29, 2009
  5. Asen Bozhilov

    Square bracket and dot notations?

    Asen Bozhilov, Jun 11, 2011, in forum: Python
    Replies:
    4
    Views:
    1,016
    Terry Reedy
    Jun 11, 2011
Loading...

Share This Page