SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot Saj's Avatar
    Join Date
    May 2003
    Location
    USA
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Basic Regexp with variables

    I'm trying to replace a letter specified by they user in a string...aka a cryptogram solver, kinda.

    Code:
              oldstring = document.getElementById("cryptogram").elements["oldstring"].value;
              oldletter = document.getElementById("cryptogram").elements["oldletter"].value;
              newletter = document.getElementById("cryptogram").elements["newletter"].value;
              regexp = new RegExp(oldletter, "gi");
              
              newstring = oldstring.replace(regexp, newletter);
    I'm trying to get something like this: "/"+ oldletter +"/gi" but obviously it wont work like that.....any help to get this working would be appreciated.

    Thanks

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks OK from here...what's happening there?
    ::: certified wild guess :::

  3. #3
    SitePoint Zealot Saj's Avatar
    Join Date
    May 2003
    Location
    USA
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I always do this...I always cpoy parts of the code onto the forum and change it to make it work...I'm sure I tried what I posted up here, but when I just added ""'s to the gi, it started working for some odd reason...

    Thanks for reading anyway
    - Saj

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's because it's a string argument to the RegExp constructor (function).

    You're welcome , I like reading things.
    ::: certified wild guess :::


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
  •