SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    ********* Ornithologist AtomicPenguin's Avatar
    Join Date
    May 2002
    Location
    Vancouver, BC
    Posts
    459
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    variables in regular expressions

    Another question:

    I have a function that accepts as an argument a number. Now what I'm trying to do is search a pre-existing string to find out whether it contains the number already. I've tried simply placing the incoming number within the regular expression, but it doesn't seem to understand it. Do I have to somehow TELL the regular expression it's a variable? Confusing...!

  2. #2
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't know what your function is looking like, but this example works fine without doing anything special:
    <SCRIPT>
    var myString = "Have 8 nice day!";
    var myNumber = 7;
    if (myString.indexOf(myNumber)>=0) alert("yes");
    else alert("nope");
    </SCRIPT>

    If var myNumber is present in var myString you should get a "yes" alert...


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
  •