Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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 regexp正常工作_Javascript_Regex - Fatal编程技术网

我无法使javascript regexp正常工作

我无法使javascript regexp正常工作,javascript,regex,Javascript,Regex,我需要在javascript中解析用户在html文本字段中输入的值 这是我第一次体验regexp 这是我的密码: var s = 'research library "not available" author:"Bernard Shaw"'; var tableau = s.split(/(?:[^\s"]+|"[^"]*")/); for (var i=0; i<tableau.length; i++) { document.write("tableau[" + i + "]

我需要在javascript中解析用户在html文本字段中输入的值

这是我第一次体验regexp

这是我的密码:

var s = 'research library "not available" author:"Bernard Shaw"';
var tableau = s.split(/(?:[^\s"]+|"[^"]*")/);
for (var i=0; i<tableau.length; i++) {
    document.write("tableau[" + i + "] = " + tableau[i] + "<BR>");
}
但我得到的却是:

tableau[0] =
tableau[1] =
tableau[2] =
tableau[3] =
tableau[4] =
tableau[5] = 
实际上,我真正需要的是分割这个值:

research library "not available" author:"Bernard Shaw"
在该阵列中:

tableau[0] = research
tableau[1] = library
tableau[2] = "not available"
tableau[3] = author:"Bernard Shaw"
但我认为javascript或类似的东西中的正向查找存在问题

我做了很多尝试,但都没有成功:


我想我真的需要一些帮助…

看起来你想在双引号外的空白处拆分。在这种情况下,您可以尝试以下正则表达式:

var tableau = s.split(/\s(?=(?:[^"]*"[^"]*")*[^"]*$)/);
这将在空格上拆分,后面是偶数个双引号

说明:

\s          # Split on whitespace
(?=         # Followed by
   (?:      # Non-capture group with 2 quotes
     [^"]*  # 0 or more non-quote characters
     "      # 1 quote
     [^"]*  # 0 or more non-quote characters
     "      # 1 quote
   )*       # 0 or more repetition of previous group(multiple of 2 quotes will be even)
   [^"]*    # Finally 0 or more non-quotes
   $        # Till the end  (This is necessary)
)      
这将为您提供最终所需的输出:

tableau[0] = research
tableau[1] = library
tableau[2] = "not available"
tableau[3] = author:"Bernard Shaw"

正则表达式可能不是一个好办法。相反,您可以编写一个小型解析器,一次遍历一个字符并构建一个数组。类似这样的内容():


返回
[“研究”、“库”、“不可用”、“作者:Bernard Shaw”]

您也可以匹配字符串

var output=s.match(/"[^"]*"|\S+/g);

您的正则表达式看起来更像是您应该查找匹配项,而不是执行拆分。。。拆分删除正则表达式匹配项并返回它们之前、之间和之后的内容。
function parse(str) {
    var arr = [];
    var quote = false;  // true means we're inside a quoted field

    // iterate over each character, keep track of current field index (i)
    for (var i = c = 0; c < str.length; c++) {
        var cc = str[c], nc = str[c+1];  // current character, next character
        arr[i] = arr[i] || '';           // create a new array value (start with empty string) if necessary

        // If it's just one quotation mark, begin/end quoted field
        if (cc == '"') { quote = !quote; continue; }

        // If it's a space, and we're not in a quoted field, move on to the next field
        if (cc == ' ' && !quote) { ++i; continue; }

        // Otherwise, append the current character to the current field
        arr[i] += cc;
    }

    return arr;
}
parse('research library "not available" author:"Bernard Shaw"')
var output=s.match(/"[^"]*"|\S+/g);