1

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 \".

4
  • 1
    what 3 words do you want? - Can you provide some examples? Commented Dec 29, 2009 at 1:44
  • 1
    presumably poster wants: hl=windows, xp, "windows xp" Commented Dec 29, 2009 at 1:46
  • which four words are you seeing? Commented Dec 29, 2009 at 1:47
  • Do you have single quotes or double quotes around the string? The hl=... line is missing these quotes. Commented Dec 29, 2009 at 1:51

3 Answers 3

2

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;
});
Sign up to request clarification or add additional context in comments.

5 Comments

it works excellent. but how do u write it as a function so i can put a string and get out an array?
it didnt work on åäö and digits so i changed the \w to [a-öA-Ö0-9]
You can also use \S instead of \w.
is there an easy way of rewriting this to a function?
Put everything into a function like function splitKeywords(keywords) { … } and replace keywords = with return.
2
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"]

2 Comments

isnt it better to just use an appropiate regular expression in split()?
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
2
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, "")
});

6 Comments

isnt it better to just use an appropiate regular expression in split()?
Believe it or not, some things are tricky to do with regexps (but see Gumbo's answer).
what does all the letters after the / do? eg /g. i cant find it in any regexp explanations
and how do i use your online if i got a string = "mac os" windows
It means that it is a global, basically find all matches in the string.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.