Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中,用引号分割文本,但不使用空格_Javascript_Regex - Fatal编程技术网

在javascript中,用引号分割文本,但不使用空格

在javascript中,用引号分割文本,但不使用空格,javascript,regex,Javascript,Regex,如何仅用引号拆分给定文本?例如: 'He said "This is true", then added "Lets go"' 将按如下方式进行拆分: ['He said', '"This is true"', ', then added', '"Lets go"'] 感谢您的帮助。您可以将match与基于交替的正则表达式一起使用: 他说“这是真的”,然后加上“放手” var arr=str.match(/“[^”]*“|[^”]+/g); 控制台日志(arr)

如何仅用引号拆分给定文本?例如:

'He said "This is true", then added "Lets go"'
将按如下方式进行拆分:

['He said', '"This is true"', ', then added', '"Lets go"']

感谢您的帮助。

您可以将
match
与基于交替的正则表达式一起使用:

他说“这是真的”,然后加上“放手” var arr=str.match(/“[^”]*“|[^”]+/g); 控制台日志(arr)到目前为止您尝试了什么?我想您正在寻找split方法:str='Some string with“double quotes.”str.split('\“')我正在使用\“确保”将其解释为char@messerbill我使用以下正则表达式设法匹配它们:
test.match(/(?警告,使用拆分将无法捕获数组中的双引号function@anubhava这似乎是正确的。谢谢!
    var text = 'He said "This is true", then added "Lets go"';
    var indexes = [];
    for (var i = 0; i < text.length; i++) {
        if (text[i] === '"') indexes.push(i);
    }
    var array = [];
    array.push(text.substring(0, indexes[0] + 1));
    for (var i = 0; i < indexes.length; i++) {
        array.push(text.substring(indexes[i], indexes[i + 1]));
    }