Page 1 of 1

javascript regex multiple matches into a javascript array

Posted: Tue Apr 17, 2007 6:43 pm
by jamma-pcb
Hi all

My first post so bare with me!

This is a javascript question

Is it possible to use regex in a form to match the first match then behind the scenes. Match all occurrences (there could be like many identical matches) and copy them into a javascript array feeding them into a pull down form.

(AJAX style)

so say a regex of

123(.*)abc

with content like this

123Eabc
123Fabc
123Gabc
123Habc
123Iabc

The javascript array would contain

0->E
1->F
2->G
3->H
4->I

Then these results displayed in a pulldown form

Any help or advice (or even solution would be most appreciated)

Thanks

Posted: Tue Apr 17, 2007 8:57 pm
by Kieran Huggins
nothing seems wrong with your method - what have you tried so far?

also... ajax refers to sending or receiving information from outside the page with an xmlHTTPrequest call.. you're just using "DOM scripting" (sorry to disappoint)

Posted: Wed Apr 18, 2007 7:28 am
by jamma-pcb
Hi there

Thats the thing I'm after a solution to this little problem or even a idea towards this

Any DOM idea or solution would be appreciated.

Posted: Wed Apr 18, 2007 7:44 am
by Chris Corbyn
It goes something like this:

Code: Select all

var backRefs = new Array();

var testString = '123Aabc\n' +
  '123Babc\n' +
  '123Cabc\n' +
  '123Dabc\n' +
  '123Eabc\n';
  
var re = /123(.*?)abc/g;

var matches;

while (matches = re.exec(testString)) {
  backRefs.push(matches);
}

/*
Array (
  0 => Array (
    0 => 123Aabc,
    1 => A
  ),
  Array (
    0 => 123Babc,
    1 => B
  ),
  Array (
    0 => 123Cabc,
    1 => C
  ),
  Array (
    0 => 123Dabc,
    1 => D
  ),
  Array (
    0 => 123Eabc,
    1 => E
  )
)

*/