javascript regex multiple matches into a javascript array

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
jamma-pcb
Forum Newbie
Posts: 2
Joined: Tue Apr 17, 2007 6:35 pm

javascript regex multiple matches into a javascript array

Post 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
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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)
jamma-pcb
Forum Newbie
Posts: 2
Joined: Tue Apr 17, 2007 6:35 pm

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
  )
)

*/
Post Reply