最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Split string into words with whitespace unless in between a pair of double quotation marks - Stack Overflow

programmeradmin2浏览0评论

I want to split this string:

get "something" from "any site"

to array. I've done that:

var array = $(this).val().replace(/\s+/g, ' ').split(" ");

But I don't want to split words in quotation marks ("").

whether it can be done in a simple way?

I want to split this string:

get "something" from "any site"

to array. I've done that:

var array = $(this).val().replace(/\s+/g, ' ').split(" ");

But I don't want to split words in quotation marks ("").

whether it can be done in a simple way?

Share Improve this question edited Jun 2, 2020 at 10:56 Wiktor Stribiżew 627k41 gold badges496 silver badges611 bronze badges asked Sep 9, 2013 at 17:25 PiotrekPiotrek 11.2k21 gold badges79 silver badges138 bronze badges 2
  • 6 take a look at this question. might help – ddavison Commented Sep 9, 2013 at 17:27
  • @dystroy: Of course it does preserve them! The answer also includes a modification on how to remove them – Bergi Commented Sep 9, 2013 at 18:04
Add a comment  | 

4 Answers 4

Reset to default 15

A solution :

var str = 'get "something" from "any site"';
var tokens = [].concat.apply([], str.split('"').map(function(v,i){
   return i%2 ? v : v.split(' ')
})).filter(Boolean);

Result :

["get", "something", "from", "any site"]

It's probably possible to do simpler. The idea here is to split using " and then split by the space the odd results of the first splitting.

If you want to keep the quotes, you may use

var tokens = [].concat.apply([], str.split('"').map(function(v,i){
     return i%2 ? '"'+v+'"' : v.split(' ')
})).filter(Boolean);

Result :

['get', '"something"', 'from', '"any site"']

Here is how to do it with a regular expression:

("\[a-zA-Z\s\]+"|\[a-zA-Z\]+)/g : Explanation of the expression is at the link.

Here is how you would use it:

var re = /([a-zA-Z]+)|("[a-zA-Z\s]+"?)\s?/g; 
var str = 'get "something" from "any site"';
var match = re.exec(str);
alert(match[1]); \\ this will give you the first matched group
                   \\ in this case it would be the word "get"

Here is another approach (demo)

function extract(input) {
    var elements = input.split(/([^\"]\S*|\".+?\")\s*/),
        matches = [];
    for(index in elements) {
        if(elements[index].length > 0) {
            if(elements[index].charAt(0) === '"') {
                matches.push(elements[index].substring(1, elements[index].length-1));
            } else {
                matches.push(elements[index]);
            }
        }
    }
    return matches;
}
alert(extract('get "something" from "any site"'))

A very simple answer to this :

'get "something" from "any site"'.split(/"/);

["get ", "something", " from ", "any site", ""]

:)

发布评论

评论列表(0)

  1. 暂无评论