SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Search Problem

  1. #1
    SitePoint Addict
    Join Date
    May 2007
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Search Problem

    Hello. I am searching a string for a phrase. Afaik the below code should return 0 but it returns -1. What is the problem here?

    PHP Code:
    alert("polo?solo".search("polo?sol")); 
    Thanks.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,147
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    Code:
    alert("polo?solo".search(/polo\?sol/));

  3. #3
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,145
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    2 problems.
    Don't use quotes to delimit the expression.
    A question mark is a special character, in this case meaning 0 or 1 "o".
    So it would match polsolo or polosolo but not polo?solo.
    If you want to match a literal question mark it needs to be escaped in the expression.

  4. #4
    SitePoint Addict
    Join Date
    May 2007
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oddz View Post
    Code:
    alert("polo?solo".search(/polo\?sol/));
    Thanks. But why string version creates problem? Cant I use strings in search?

  5. #5
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,145
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    search() takes an expression, not a string. Maybe you're thinking of indexOf() ??
    Code:
    alert("polo?solo".indexOf("polo?sol"));

  6. #6
    SitePoint Addict
    Join Date
    May 2007
    Posts
    242
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    search() takes an expression, not a string. Maybe you're thinking of indexOf() ??
    Oh I didnt know that. Thanks for clarifying.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •