Solved

Regex to split

Posted on 2011-02-16
8
243 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

828 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