Solved

Regex to split

Posted on 2011-02-16
8
239 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

744 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

10 Experts available now in Live!

Get 1:1 Help Now