1

Say I have:

var str = "this is a string separated by spaces";

and I did:

alert(str.split(" " , 1));

the outcome would be "this"

Whereas I want the outcome to be "this,is a string separated by spaces"

maybe split isn't the right method?

What I'm trying to do is separate a string into parts based on semicolons, unless those semicolons are in quotes. For example, I would want

randomnessstuff;morestuff;some more stuff

to be in three parts, so I've been doing:

var str = "randomnessstuff;morestuff;some more stuff";
var parts = str.split(";");

Which has been working fine, but if the semicolon is in quotes, I want it to NOT be separated into another part.

for example, with:

var str = "randomnessstuff;morestuff and a semicolon in quotes : ";";some more stuff";

I would want part 1 to be randomnessstuff , part 2 to be morestuff and a semicolon in quotes : ";" , and part 3 to be some more stuff

of course, if I just did split with the semicolon again, it would make part 3 the quote.

What I'm hoping to do is have a loop that checks the semicolons one by one to see if they're in quotes, and if not, to split with them. If this last bit didn't make any sense, then please just answer the first question, about using split without losing the rest of the string.

mowwwalker
  • 16,634
  • 25
  • 104
  • 157
  • Are you trying to parse CSV data with the semicolon as the delimiter? If so, check out http://stackoverflow.com/questions/1293147/javascript-code-to-parse-csv-data. – Ray Toal Jul 21 '11 at 06:49
  • It's for an attempt at a basic emulator of a game's console, where commands are separated by semicolons. Although the semicolons separate the commands, the commands can have semicolons in them. – mowwwalker Jul 21 '11 at 06:54
  • If language used by this console is yours, i suggest changing language structure, for example invalidating semicolons from commands, or disallow more than one command in a line. If semicolons in strings are a must, you probably should do some kind of tokenizer for your language. – Adam Jurczyk Jul 21 '11 at 07:07

2 Answers2

1

The easy way to do this one is unfortunately manual.

var r = "randomnessstuff;morestuff and a semicolon in quotes : \";\";some more stuff"
var l = r.length
var out = []
var inQuotes = false;
function toggleQuotes(){ inQuotes = !inQuotes }
var tmp = ""
for(var i = 0; i < l; i++ ) { 
     // examine character by character.
     var chr = r.charAt(i); 
     // only handles one type of quote and no escapes currently
     if( chr == '"' ) toggleQuotes(); 
     /*
        escape might look like this:
        if( chr == '"' && r.charAt(i-1) != '\' )

        support for both types of quotes might be:
        if( chr == '"' || chr == "'" && r.charAt(i-1) != '\' )
             toggleQuotes(chr);

        then toggleQuotes would be:  
        function toggleQuotes(chr){ 
            if(inQuotes == chr) inQuotes = false;
            else inQuotes = chr
        }
     */
     if( !inQuotes && chr == ";" ) 
     {
         out.push(tmp); // store temp string
         tmp = "" // reset strubg
     } 
     else tmp += chr // append the temporary string
}
out.push(tmp) // the remainder needs to be added still.
console.log(out) 
//["randomnessstuff", 
// "morestuff and a semicolon in quotes : ";"", 
// "some more stuff"]
cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
  • That's pretty god damn genius. I think I'll use it, thanks so much!! – mowwwalker Jul 21 '11 at 07:06
  • 1
    I once built a whole "command line" in ActionScript 2. There was a lot of this type of stuff. – cwallenpoole Jul 21 '11 at 07:06
  • lol@ strubg. Nothing is so weird as the words you type when your fingers aren't aligned correctly on the keyboard. – mowwwalker Jul 21 '11 at 07:09
  • Also, is log() an actual method, or did you put that as an example? – mowwwalker Jul 21 '11 at 07:10
  • I'm not really comfortable giving you the AS2 version (the code actually is really tied into some proprietary stuff to increase its functionality), but I wrote a similar AS3 version at about the same time and published it on ActionScript.org: http://www.actionscript.org/resources/articles/793/1/Basic-debugging-functionality----Part-1/Page1.html http://www.actionscript.org/resources/articles/800/1/Basic-debugging-functionality----Part-1b-the-Console/Page1.html – cwallenpoole Jul 21 '11 at 07:13
  • console.log is a method available with Firebug and in Chrome, I think it is also available with certain extensions on IE. – cwallenpoole Jul 21 '11 at 07:14
0

UPDATED

Here's a more practical solution to parsing this. Encode the part you don't want split by replacing it with a code so here I'm translating ";" to "||" now you can split with just a .split(';') method, then replace the quoted semicolon back to ";" when iterating the split strings.

You could change this "||" code to any code like some funky unicode characters, pairs of scissors perhaps?!? ✁✁ :)

example: http://jsfiddle.net/pxfunc/qXrVr/5/

var str = "randomnessstuff;morestuff and a semicolon in quotes : \";\";some more stuff",
    results = document.getElementById("results"),
    quotedSemicolon = '";"',
    quotedSemicolonCode = '"||"'; // use bar bar as a replacement code for quoted semicolons

results.innerHTML = "";

// replace then split the string to an array object
var strArr = str.replace(quotedSemicolon, quotedSemicolonCode).split(';');
results.innerHTML += "<b>Split</b><br>";
for (s in strArr) {
    // replace back the quoted semicolons
    results.innerHTML += strArr[s].replace(quotedSemicolonCode, quotedSemicolon) + '<br>';
}

previous solution http://jsfiddle.net/pxfunc/qXrVr/1/ used regex /(;)(?![";"])/ to split ... nevermind that though ... the above is simpler

MikeM
  • 27,227
  • 4
  • 64
  • 80
  • Can you explain this a little more? It looks like gibberish to me. I don't get `/(;)(?![";"])/`. To me, it looks like a bunch of random symbols :( – mowwwalker Jul 21 '11 at 07:23
  • updated my answer to a more practical solution, no need for regex – MikeM Jul 21 '11 at 15:34
  • Sorry, I think I didn't explain this well enough, but what I meant when I referred to semicolons in quotes wasn't the exact string `";"`, but rather a semicolon anywhere in quotes, like `"asdfasdf;asdfasdf"`. – mowwwalker Jul 21 '11 at 19:07
  • Are there multiple semicolons within the quotes `"a;b;c"`? – MikeM Jul 21 '11 at 20:19