+ Reply to Thread
Results 1 to 6 of 6

Thread: JavaScript RegEx $1 Tokens

  1. #1
    masterjake is offline x10Hosting Member masterjake is an unknown quantity at this point
    Join Date
    Oct 2007
    Posts
    73

    JavaScript RegEx $1 Tokens

    Hi. I'm having a problem. I have various text fields with ID's of a letter and a number e.g. "A1", "C3", etc.

    When this JavaScript function runs it is suppose to replace all occurrences of "{stuff here}" with the value at the cell number within the {}.

    For example, if they type in "{A1}+{B2}" then the JavaScript should find {A1} and replace it with the value of the element who's ID is "A1."

    Here is what I have. I know it's a problem with the "$1."

    Code:
    var editedExpression = eqvalue.replace(/\{(.+?)\}/g, document.getElementById("$1").value);

  2. #2
    VPmase's Avatar
    VPmase is offline x10 Elder VPmase is an unknown quantity at this point
    Join Date
    Nov 2007
    Location
    Dixon, IL, USA
    Posts
    914

    Re: JavaScript RegEx $1 Tokens

    Where does the $1 come in other than that line?

  3. #3
    ah-blabla's Avatar
    ah-blabla is offline x10 Lieutenant ah-blabla is an unknown quantity at this point
    Join Date
    Sep 2009
    Posts
    375

    Re: JavaScript RegEx $1 Tokens

    //UPDATE: Ignore this post, misson actually knows what he's on about, so read his post.

    Quote Originally Posted by VPmase View Post
    Where does the $1 come in other than that line?
    I was wondering the same. If $1 is a variable, then remove the quotation marks around it, since here you are passing $1 as the name of an element. However, javascript variables aren't usually written with $ signs... Unless you are using a variable from php, in which case you would have to enclose it in <?php ?> tags if it is external, or add a . on either side of the $1 to concatenate it as part of a string you are doing something with.
    Last edited by ah-blabla; 11-01-2009 at 10:24 AM.

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: JavaScript RegEx $1 Tokens

    document.getElementById(...) is being called before eqvalue.replace(...) (remember, in f(..., g(...), ...), the call to g(...) must be evaluated before f is called). The solution is to use an anonymous function:

    Code:
    var editedExpression = eqvalue.replace(/\{(.+?)\}/g, 
            function(matched, id) { return document.getElementById(id).value; });
    The matched substrings are passed as parameters to the function; $& is passed in arguments[0] and $n in arguments[n].

    Also note that the $n signifier for positional parameters is only supported in a few contexts: a regular expression and a plain string replacement. There are also properties of RegExp named $n that hold the matches from the last successful match; while they have the same signifiers, they're different beasts. "$n" has no special meaning within a function replacement.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  5. #5
    masterjake is offline x10Hosting Member masterjake is an unknown quantity at this point
    Join Date
    Oct 2007
    Posts
    73

    Re: JavaScript RegEx $1 Tokens

    Thanks man, that code worked.

    In every other language I've used for RegEx replacement (generally by means of preg_replace), all of the returned (groups) were placed into $variables.

    I figured JavaScript would be the same.

  6. #6
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: JavaScript RegEx $1 Tokens

    Quote Originally Posted by masterjake View Post
    In every other language I've used for RegEx replacement (generally by means of preg_replace), all of the returned (groups) were placed into $variables.
    True of Perl, but not PHP. "$1" isn't a valid PHP variable name; PHP variable names must start with an underscore or a letter. Backreferences of the form "$n" are only supported in regular expressions and replacement strings.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. Making a site JavaScript dependent - pros/cons?
    By Tarzan in forum Programming Help
    Replies: 8
    Last Post: 07-11-2008, 10:08 AM
  2. drop down menus with JavaScript disabled?
    By sifaka in forum Free Hosting
    Replies: 1
    Last Post: 05-15-2008, 10:46 AM
  3. a little javascript regex help
    By MasterMax1313 in forum Programming Help
    Replies: 2
    Last Post: 05-02-2008, 09:00 AM
  4. javascript and external javascript files problem
    By delon in forum Programming Help
    Replies: 6
    Last Post: 04-27-2008, 12:41 AM
  5. XML and Javascript
    By cuteboytm in forum Graphics & Webdesign
    Replies: 1
    Last Post: 09-21-2007, 10:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers