我不能让我的javascript regexp工作

时间:2013-09-18 16:00:04

标签: javascript regex

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

这是我的第一次正则表达式体验。

这是我的代码:

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] = research
tableau[1] = library
tableau[2] = "not available"
tableau[3] = author:
tableau[4] = "Bernard Shaw"

但我得到了这个:

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或类似的东西中存在正面观察的问题。

我做了很多尝试而没有取得更多成功:

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

3 个答案:

答案 0 :(得分:2)

好像你想拆分双引号之外的空格。在这种情况下,你可以试试这个正则表达式:

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"

答案 1 :(得分:0)

正则表达可能不是你要走的路。相反,您可能会编写一个小型解析器,一次沿着一个角色进行游戏并构建一个数组。这样的事情(http://jsfiddle.net/WTMct/1):

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"')

返回["research", "library", "not available", "author:Bernard Shaw"]

答案 2 :(得分:0)

您还可以匹配字符串

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