Javascript JS split()函数,该函数忽略出现在引号内的分隔符

Javascript JS split()函数,该函数忽略出现在引号内的分隔符,javascript,split,quotes,Javascript,Split,Quotes,基本上,就像你要说 var string = 'he said "Hello World"'; var splitted = string.split(" "); 拆分的阵列将是: 'he' 'said' '"Hello World"' 基本上,将带引号的部分视为单独的项目 那么我该如何在javascript中实现这一点呢?我是否需要一个for循环来检查字符串是否在一组引号内?或者有更简单的方法吗?您可以使用正则表达式: var splitted = string.match(/(".*?"

基本上,就像你要说

var string = 'he said "Hello World"';
var splitted = string.split(" ");
拆分的阵列将是:

'he' 'said' '"Hello World"'
基本上,将带引号的部分视为单独的项目


那么我该如何在javascript中实现这一点呢?我是否需要一个for循环来检查字符串是否在一组引号内?或者有更简单的方法吗?

您可以使用正则表达式:

var splitted = string.match(/(".*?")|(\S+)/g);
基本上,它首先搜索引号(包括空格)之间包含任何字符的字符串,然后搜索字符串中的所有剩余单词

比如说

var string='这不是一个没有引号的字符串'; string.match/*?|\S+/g

将其返回到控制台:

[""This is"", "not", "a", "string", ""without"", ""quotes in it""]

您可以使用正则表达式:

var splitted = string.match(/(".*?")|(\S+)/g);
基本上,它首先搜索引号(包括空格)之间包含任何字符的字符串,然后搜索字符串中的所有剩余单词

比如说

var string='这不是一个没有引号的字符串'; string.match/*?|\S+/g

将其返回到控制台:

[""This is"", "not", "a", "string", ""without"", ""quotes in it""]

首先,我想你的意思是:

var string = 'he said "Hello World"';
现在我们已经解决了这个问题,您对for循环的想法部分是正确的。我会这样做:

// initialize the variables we'll use here
var string = 'he said "Hello World"', splitted = [], quotedString = "", insideQuotes = false;

string = string.split("");

// loop through string in reverse and remove everything inside of quotes
for(var i = string.length; i >= 0; i--) {
    // if this character is a quote, then we're inside a quoted section
    if(string[i] == '"') {
        insideQuotes = true;
    }

    // if we're inside quotes, add this character to the current quoted string and
    // remove it from the total string
    if(insideQuotes) {
        if(string[i] == '"' && quotedString.length > 0) {
            insideQuotes = false;
        }

        quotedString += string[i];
        string.splice(i, 1);
    }

    // if we've just exited a quoted section, add the quoted string to the array of
    // quoted strings and set it to empty again to search for more quoted sections
    if(!insideQuotes && quotedString.length > 0) {
        splitted.push(quotedString.split("").reverse().join(""));
        quotedString = "";
    }
}

// rejoin the string and split the remaining string (everything not in quotes) on spaces
string = string.join("");
var remainingSplit = string.split(" ");

// get rid of excess spaces
for(var i = 0; i<remainingSplit.length; i++) {
    if(remainingSplit[i].length == " ") {
        remainingSplit.splice(i, 1);
    }
}

// finally, log our splitted string with everything inside quotes _not_ split
splitted = remainingSplit.concat(splitted);
console.log(splitted);​

我相信有更有效的方法,但这会产生与您指定的完全相同的输出。JSFIDLE中的一个工作版本的链接。

首先,我认为您的意思是:

var string = 'he said "Hello World"';
现在我们已经解决了这个问题,您对for循环的想法部分是正确的。我会这样做:

// initialize the variables we'll use here
var string = 'he said "Hello World"', splitted = [], quotedString = "", insideQuotes = false;

string = string.split("");

// loop through string in reverse and remove everything inside of quotes
for(var i = string.length; i >= 0; i--) {
    // if this character is a quote, then we're inside a quoted section
    if(string[i] == '"') {
        insideQuotes = true;
    }

    // if we're inside quotes, add this character to the current quoted string and
    // remove it from the total string
    if(insideQuotes) {
        if(string[i] == '"' && quotedString.length > 0) {
            insideQuotes = false;
        }

        quotedString += string[i];
        string.splice(i, 1);
    }

    // if we've just exited a quoted section, add the quoted string to the array of
    // quoted strings and set it to empty again to search for more quoted sections
    if(!insideQuotes && quotedString.length > 0) {
        splitted.push(quotedString.split("").reverse().join(""));
        quotedString = "";
    }
}

// rejoin the string and split the remaining string (everything not in quotes) on spaces
string = string.join("");
var remainingSplit = string.split(" ");

// get rid of excess spaces
for(var i = 0; i<remainingSplit.length; i++) {
    if(remainingSplit[i].length == " ") {
        remainingSplit.splice(i, 1);
    }
}

// finally, log our splitted string with everything inside quotes _not_ split
splitted = remainingSplit.concat(splitted);
console.log(splitted);​


我相信有更有效的方法,但这会产生与您指定的完全相同的输出。在jsFiddle中有一个指向这个工作版本的链接。

这不是有效的JavaScript。SyntaxError:我知道,是意外的身份证明。我是用一种假设的方式输入的,整个“他说你好”的东西是一个完整的字符串。你想处理转义引号吗?顺便说一句,解析器更容易编写:-我不太清楚你的意思,但脚本基本上会将引号的d部分作为一个单独的itemVar字符串='他说你好世界';?那是有效的,那不是有效的JavaScript。SyntaxError:我知道,是意外的身份证明。我是用一种假设的方式输入的,整个“他说你好”的东西是一个完整的字符串。你想处理转义引号吗?顺便说一句,解析器更容易编写:-我不太清楚你的意思,但脚本基本上会将引号的d部分作为一个单独的itemVar字符串='他说你好世界';?这是正确的,+1似乎符合OP的要求。FWIW,您可以将[^\s]+缩短为\s+。比我的解决方案更简洁+1!@如果你想在下一场比赛中停下来,你不会想要一场贪婪的比赛。@Christophe是的,这确实是有道理的。谢谢因为我使用的是.*这意味着,匹配任何字符,它将尝试使用尽可能多的字符,包括引号!通过添加问号,我强制它在找到的第一个引号处停止。在这种情况下,问号被称为惰性量词,而不是正常的贪婪匹配。另一种实现方法是使用[^]*而不是。*=match all,但quotes+1似乎符合OP的要求。FWIW,您可以将[^\s]+缩短为\s+。比我的解决方案更简洁+1!@如果你想在下一场比赛中停下来,你不会想要一场贪婪的比赛。@Christophe是的,这确实是有道理的。谢谢因为我使用的是.*这意味着,匹配任何字符,它将尝试使用尽可能多的字符,包括引号!通过添加问号,我强制它在找到的第一个引号处停止。在这种情况下,问号被称为惰性量词,而不是正常的贪婪匹配。实现它的另一种方法是使用[^]*而不是。*=match all,但quotesHanks除外。如果人们没有更简单的选择,我可能会使用这个方法。这运行得很快,对吧?谢谢,如果人们没有更简单的选择,我可能会用这个。这个跑得很快,对吧?