tags:

views:

172

answers:

3

I have to split this string here in JavaScript:

 hl=windows xp \"windows xp\"

to three words.

I used:

 keywords = keywords.split(/ /);

But then i got 4 words:

windows
xp
\"windows
xp\"

How could i split this string to just 3 words?

EDIT: and how do i remove the \".

+2  A: 
function split(s, on) {
    var words = [];
    var word = "";
    var instring = false;
    for (var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (c == on && !instring) {
            words.push(word);
            word = "";
        } else {
            if (c != '"')
                word += c;
        }
        if (c == '"') instring = !instring;
    }
    if (word != '') words.push(word); //dangling word problem solved
    return words;
}

and a usage example that fits the OP's example

var hl = "windows xp \"windows xp\"";
split(hl, " "); // returns ["windows","xp","windows xp"]
barkmadley
isnt it better to just use an appropiate regular expression in split()?
weng
didnt work. it returned windows, xp
weng
the problem with the regular expression approach is that your problem is not regular enough for regular expression to easily express. fixed the dangling word issue, thats what i get for not testing
barkmadley
+2  A: 
hl="windows xp \"windows xp\""

var results = hl.split(/ /);

for(var s in results) {
    try {
        while (results[s].search("\"") > -1) {
            results.splice(s, 1);
        }
    } catch(e) {}
}

var quoted = hl.match(/"[^"]*"/g); //matches quoted strings

for(var s in quoted) {
    results.push(quoted[s]);
}

//results = ["windows", "xp", ""windows xp""]

edit, a one liner:

var results = hl.match(/\w+|"[^"]*"/g).map(function(s) {
    return s.replace(/^"+|"+$/g, "")
});
sworoc
isnt it better to just use an appropiate regular expression in split()?
weng
but it was a good and working function though. thx
weng
Believe it or not, some things are tricky to do with regexps (but see Gumbo's answer).
outis
what does all the letters after the / do? eg /g. i cant find it in any regexp explanations
weng
and how do i use your online if i got a string = "mac os" windows
weng
It means that it is a global, basically find all matches in the string.
sworoc
For reference RE: /g modifier: https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/RegExp, https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Working_with_Regular_Expressions/Advanced_Searching_With_Flags
outis
+2  A: 

Here’s a simple one:

keywords = keywords.match(/"[^"]*"|\w+/g).map(function(val) {
    if (val.charAt(0) == '"' && val.charAt(val.lenngth-1) == '"') {
        val = val.substr(1, val.length-2);
    }
    return val;
});
Gumbo
it works excellent. but how do u write it as a function so i can put a string and get out an array?
weng
it didnt work on åäö and digits so i changed the \w to [a-öA-Ö0-9]
weng
You can also use `\S` instead of `\w`.
Gumbo
is there an easy way of rewriting this to a function?
weng
Put everything into a function like `function splitKeywords(keywords) { … }` and replace `keywords =` with `return`.
Gumbo