【问题标题】:How to spilt or RegExp the string?如何拆分或正则表达式字符串?
【发布时间】:2015-06-26 04:48:11
【问题描述】:

我有一个这样的错误字符串

/code/test.java:3: error: unclosed string literal\n       
System.out.println(\"Hello World!)      \n                           
^\n/code/test.java:3: error: ';' expected\n        
System.out.println(\"Hello World!)      \n   
^\n/code/test.java:5: error: reached end of file while parsing\n}\n ^\n3 errors\n

我想解析这个字符串得到两个数组

第一个数组是 [error: unclosed string literal System.out.println("Hello World!) , error: ';' expected System.out.println("Hello World!) , error: reached end of file while parsing }] 第二个数组是[3,3,5]

在这个jsfiddle,我已经得到了我想要的第一个数组但是如何得到第二个数组呢?谢谢

【问题讨论】:

    标签: javascript arrays regex


    【解决方案1】:

    获取第二个数组:

    var error_string="/code/test.java:3: error: unclosed string literal\n        System.out.println(\"Hello World!)      \n                           ^\n/code/test.java:3: error: ';' expected\n        System.out.println(\"Hello World!)      \n                                               ^\n/code/test.java:5: error: reached end of file while parsing\n}\n ^\n3 errors\n";
    var second_array = [];
    do
    {
        match = re.exec(str2);
        if(match)
            second_array.push(match[1]);
    }while(match);
    document.getElementById("str2").innerHTML = second_array.toString();
    

    FIDDLE

    【讨论】:

    • 酷!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-19
    • 2011-06-18
    相关资源
    最近更新 更多