Solved

Regex to split

Posted on 2011-02-16
8
240 Views
Last Modified: 2012-08-13
I have this string, and need to split it as follows:

HANDLER click, ".ml,.asr,.menu", MenuClick %target%, "asdasdkjfhkasdj,,sdaflijsdalfk", anything

[1] HANDLER
[2] CLICK
[3] ".ml,.asr,.menu"
[4] The rest of the string after the next comma, whatever it is ! So in this case   MenuClick %target%, "asdasdkjfhkasdj,,sdaflijsdalfk", anything

Any ideas on a regex to split (or match) this in javascript ? thanks
0
Comment
Question by:plq
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 20

Expert Comment

by:Proculopsis
ID: 34905245

I would go for a match instead.

  "[^"]*"|[^, ]+
0
 
LVL 8

Author Comment

by:plq
ID: 34905290
Thanks, thats close

Its giving this

myArray[0] = "HANDLER"
myArray[1] = "click"
myArray[2] = "".ml,.asr,.menu""
myArray[3] = "MenuClick"
myArray[4] = "%target%"
myArray[5] = ""asdasdkjfhkasdj,,sdaflijsdalfk""
myArray[6] = "anything "

And what I need is this

myArray[0] = "HANDLER"
myArray[1] = "click"
myArray[2] = "".ml,.asr,.menu""
myArray[3] = "MenuClick %target%, "asdasdkjfhkasdj,,sdaflijsdalfk", anything"

I cannot predict what will be in element [3] - it could be any text, so I don't want the regex to operate on that at all, when its on item 3, just return everything else in the string.. thanks

0
 
LVL 4

Expert Comment

by:andreizz
ID: 34905475
You can test this here:
http://www.regextester.com/

enter the text below in the Type regex: field:
(.[A-Z]+)[ ](.[a-z]+)[,][ ](".[a-z,.]+"),(.*)

Open in new window


And insert the test text in the Test on Text: field.

Good luck!
0
 
LVL 20

Accepted Solution

by:
Proculopsis earned 167 total points
ID: 34905575
I think this gets it:

var source = 'HANDLER click, ".ml,.asr,.menu", MenuClick %target%, "asdasdkjfhkasdj,,sdaflijsdalfk", anything';

var regularExpression = /(?:[, ]*)((?:"[^"]*")|[^, ]+)(?:[, ]*)((?:"[^"]*")|[^, ]+)(?:[, ]*)((?:"[^"]*")|[^, ]+)(?:[, ]*)(.*)/;

var result = source.replace( regularExpression, "$1~~~$2~~~$3~~~$4" );
result = result.split( "~~~" );
var i = -1;
while ( ++i != result.length ) alert( result[i] );

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Assisted Solution

by:andreizz
andreizz earned 166 total points
ID: 34905681
Using RegExp object:
var str = "HANDLER click, ".ml,.asr,.menu", MenuClick %target%, "asdasdkjfhkasdj,,sdaflijsdalfk", anything";
var re = new RegExp("HANDLER click, ".ml,.asr,.menu", MenuClick %target%, "asdasdkjfhkasdj,,sdaflijsdalfk", anything", "g");
var myArray = str.match(re);
Using literal:
var myArray = str.match(/HANDLER click, ".ml,.asr,.menu", MenuClick %target%, "asdasdkjfhkasdj,,sdaflijsdalfk", anything/g)

if ( myArray != null) {
for ( i = 0; i < myArray.length; i++ ) {
var result = "myArray[" + i + "] = " + myArray[i];
}
}

Open in new window

0
 
LVL 9

Assisted Solution

by:iGottZ
iGottZ earned 167 total points
ID: 34905786
^(.+?)\s+(.+?),\s+(".+?"),\s+(.+)$
\1: HANDLER
\2: click
\3: ".ml,.asr,.menu"
\4: MenuClick %target%, "asdasdkjfhkasdj,,sdaflijsdalfk", anything

Open in new window


var string = 'HANDLER click, ".ml,.asr,.menu", MenuClick %target%, "asdasdkjfhkasdj,,sdaflijsdalfk", anything';
string = string.match(/^(.+?)\s+(.+?),\s+(".+?"),\s+(.+)$/);
string.shift();
alert(string.join("\n\n"));

Open in new window

0
 
LVL 8

Author Comment

by:plq
ID: 34906700
gosh thanks for all the responses folks, you guys rock !  Let me test them out and I'll report back here in an hour or two
0
 
LVL 8

Author Closing Comment

by:plq
ID: 34942515
Sorry folks I've got pulled onto another project and this will have to be shelved for about 2 weeks. I'll split points
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now