简体   繁体   English

正则表达式在 javascript 拆分方法中不起作用,但在在线测试网站上起作用

[英]Regex not working in javascript split method but working Online test websites

i have this Regex (?=(?:(?:[^']*'){2})*[^']*$)\\\\n working on online test website perfectly but not in Below code.我有这个正则表达式(?=(?:(?:[^']*'){2})*[^']*$)\\\\n完美地在在线测试网站上工作,但不在下面的代码中。 i have a string with multiple \n and also some with in " double Quotation but i want to ignore all \n with in " double Quotes.我有一个带有多个\n的字符串,还有一些带有 in "双引号的字符串,但我想忽略所有带有 in "双引号的\n

let input = "a,b,c\n'aa\nbb\ncc'\nhello world";
    
    const lines = input.split("(?=(?:(?:[^']*'){2})*[^']*$)\\\\n");
    console.log(lines); 

i am getting this output我得到这个 output 在此处输入图像描述

want this Output:想要这个 Output:

[0]: "a,b,c"
    [1]: "aa\nbb\ncc"
    [2]: "hello world"

Why so complicated?为什么这么复杂?

 let input = "a,b,c \n 'aa\nbb\ncc' \n hello world"; console.log(input.split(' \n '))

There was some ambiguity between your explanation and your sample data.您的解释和示例数据之间存在一些歧义。 You were talking about double quotes as a delimiter in your input, but it was the opposite in the sample.您在输入中谈论双引号作为分隔符,但在示例中却相反。 There were also too many escape characters before the \n in the regex.正则表达式中的\n之前也有太多转义字符。 \\\\n becomes \n . \\\\n变为\n Not \\n because in a javascript string \n stands for the actual newline character.不是\\n因为在 javascript 字符串中\n代表实际的换行符。 No need to esacpe it in the regex.无需在正则表达式中转义它。

 let input = 'a,b,c\n"aa\nbb\ncc"\nhello world'; let lines = input.split(/(?=(?:(?:[^"]*"){2})*[^"]*$)\n/); console.log(lines);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM