Javascript regex

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

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Javascript regex

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'm having trouble understanding what you want to do...
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post 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]);
} 
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply