Javascript Javascript将空格或引号上的字符串拆分为数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2817646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:02:46  来源:igfitidea点击:

Javascript split string on space or on quotes to array

javascriptregexsplit

提问by Remi

var str = 'single words "fixed string of words"';
var astr = str.split(" "); // need fix

I would like the array to be like this:

我希望数组是这样的:

var astr = ["single", "words", "fixed string of words"];

回答by YOU

str.match(/\w+|"[^"]+"/g)

//single, words, "fixed string of words"

回答by dallin

The accepted answer is not entirely correct. It separates on non-space characters like . and - and leaves the quotes in the results. The better way to do this so that it excludes the quotes is with capturing groups, like such:

接受的答案并不完全正确。它分隔非空格字符,如 . 和 - 并在结果中留下引号。执行此操作以排除引号的更好方法是使用捕获组,例如:

//The parenthesis in the regex creates a captured group within the quotes
var myRegexp = /[^\s"]+|"([^"]*)"/gi;
var myString = 'single words "fixed string of words"';
var myArray = [];

do {
    //Each call to exec returns the next regex match as an array
    var match = myRegexp.exec(myString);
    if (match != null)
    {
        //Index 1 in the array is the captured group if it exists
        //Index 0 is the matched text, which we use if no captured group exists
        myArray.push(match[1] ? match[1] : match[0]);
    }
} while (match != null);

myArray will now contain exactly what the OP asked for:

myArray 现在将包含 OP 要求的内容:

single,words,fixed string of words

回答by Sean Kinsey

This uses a mix of split and regex matching.

这使用了拆分和正则表达式匹配的混合。

var str = 'single words "fixed string of words"';
var matches = /".+?"/.exec(str);
str = str.replace(/".+?"/, "").replace(/^\s+|\s+$/g, "");
var astr = str.split(" ");
if (matches) {
    for (var i = 0; i < matches.length; i++) {
        astr.push(matches[i].replace(/"/g, ""));
    }
}

This returns the expected result, although a single regexp should be able to do it all.

这将返回预期的结果,尽管单个正则表达式应该能够完成所有操作。

// ["single", "words", "fixed string of words"]

UpdateAnd this is the improved version of the the method proposed by S.Mark

更新这是S.Mark提出的方法的改进版本

var str = 'single words "fixed string of words"';
var aStr = str.match(/\w+|"[^"]+"/g), i = aStr.length;
while(i--){
    aStr[i] = aStr[i].replace(/"/g,"");
}
// ["single", "words", "fixed string of words"]

回答by Qian Chen

Here might be a complete solution: https://github.com/elgs/splitargs

这里可能是一个完整的解决方案:https: //github.com/elgs/splitargs

回答by Tsuneo Yoshioka

ES6 solution supporting:

ES6 解决方案支持:

  • Split by space except for inside quotes
  • Removing quotes but not for backslash escaped quotes
  • Escaped quote become quote
  • Can put quotes anywhere
  • 除内引号外,按空格分割
  • 删除引号但不用于反斜杠转义引号
  • 转义报价成为报价
  • 可以在任何地方放置引号

Code:

代码:

str.match(/\?.|^$/g).reduce((p, c) => {
        if(c === '"'){
            p.quote ^= 1;
        }else if(!p.quote && c === ' '){
            p.a.push('');
        }else{
            p.a[p.a.length-1] += c.replace(/\(.)/,"");
        }
        return  p;
    }, {a: ['']}).a

Output:

输出:

[ 'single', 'words', 'fixed string of words' ]

回答by tim.breeding

This will split it into an array and strip off the surrounding quotes from any remaining string.

这会将其拆分为一个数组,并从任何剩余的字符串中去除周围的引号。

const parseWords = (words = '') =>
    (words.match(/[^\s"]+|"([^"]*)"/gi) || []).map((word) => 
        word.replace(/^"(.+(?="$))"$/, ''))

回答by julianYaman

This soulution would work for both double (") and single (') quotes:

此解决方案适用于双 (") 和单 (') 引号:

Code:

代码

str.match(/[^\s"']+|"([^"]*)"/gmi)

// ["single", "words", "fixed string of words"]

Here it shows how this regular expression would work: https://regex101.com/r/qa3KxQ/2

这里展示了这个正则表达式的工作原理:https: //regex101.com/r/qa3KxQ/2

回答by user655489

I noticed the disappearing characters, too. I think you can include them - for example, to have it include "+" with the word, use something like "[\w\+]" instead of just "\w".

我也注意到消失的角色。我认为您可以包含它们 - 例如,要在单词中包含“+”,请使用“[\w\+]”之类的内容,而不仅仅是“\w”。