Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Javascript regex backrefering

Name: Anonymous 2012-02-22 12:29

In Perl, this will successfully backreference TeXt.


  my $str = "this is TeXt";;

  if ( str =~ /(TeXt)/g ) ) {
    $str = $1;
  }


However, if I try something similar in Javascript:



  var str = "this is TeXt";;

  if ( str.match(/(TeXt)/g) ) {
    str = $1;
  }


This will not backreference TeXt

Any idea how I can access the captured group within the If block in Javascript?

Name: Anonymous 2012-02-22 12:47

But this isn't back-referring at all
str.match should return an object, you can access the matched groups somewhere there.

Name: Anonymous 2012-02-22 13:01

>>2
I have no idea how to do that. Am I able to do the same thing as in the Perl example? I just want $1 to be what was captured in the If match statement.

Name: Anonymous 2012-02-22 13:59

>>3
Read SICP.

Name: Anonymous 2012-02-22 15:46

Why fuck around with Javascript when you can learn D?

Name: Anonymous 2012-02-22 15:48

>>5
D is shit,stop spamming

Name: Anonymous 2012-02-22 17:17

>>3
The way Perl puts stuff in $1 and such is by having global variables(!) that get assigned to by the regexp parser. Javascript doesn't do this thank god. Your code should instead look something like this:

var matches = str.match(/(TeXt/g);
if (matches) {
    // do something with matches
}


You should look up the documentation for this on Mozilla's developer site (https://developer.mozilla.org/en/JavaScript).

Name: Anonymous 2012-02-22 19:25

>>7
thanks, that was very helpful

Name: Anonymous 2012-02-23 16:31

Use Jison. It's a JavaScript port of Bison.
http://zaach.github.com/jison/docs/

Don't change these.
Name: Email:
Entire Thread Thread List