Page 1 of 1

Javascript regex

Posted: Tue Oct 30, 2007 5:07 am
by shiznatix
I have an associative PHP array that is all on 1 line. I want to get this array into a javascript array so what I figured I could do would be to split it up with regex. Basically I am trying to split it on something like this:

[ANY_2_UNDERCASE_LETTERS_A_TO_Z] =>

so a php array:

Code: Select all

Array ( [en] => stuff stuff stuff [ee] => asja asja asja )
would get split into "stuff stuff stuff" and "asja asja asja". At least that is what I want. I have started trying to write the regex but it is not working at all. This is what I have (which returns no results)

Code: Select all

var expression = /(\[[a-z]\])/g;
var thing = expression.exec(text[7]);//text[7] is the 1 lined php array

for (var i = 0; i < thing.length; i++)
{
	alert(thing[i]);
}
any help is appreciated

Posted: Tue Oct 30, 2007 10:19 am
by feyd
I'm having trouble understanding what you want to do...

Posted: Tue Oct 30, 2007 12:52 pm
by VladSun
Do you mean this:

Code: Select all

var text = "[en] => stuff stuff stuff [ee] => asja asja asja";

var expression = /\[[a-z]{2}\] =>(.+?)/g;
var thing = text.split(expression);

for (var i = 0; i < thing.length; i++)
{
        alert(thing[i]);
}