【问题标题】:How to correctly check if a string match regex in javascript如何正确检查字符串是否与javascript中的正则表达式匹配
【发布时间】:2021-12-18 18:15:15
【问题描述】:

我想检查一个字符串是否匹配以下模式

文本包含两个",每个文本可以通过; 分隔,附加;是强制性的

我尝试使用正则表达式/"(.*?)";?/,但无法正常工作

test3下面的代码sn-p中返回true但不应该匹配正则表达式

我不知道这是我的正则表达式还是我尝试我的正则表达式的方式

const regex = /"(.*?)";?/;


const test = `"hello"; "world";`;
const test2 = `"hello"; "world"`;

const test3 = `"hello; "world"`;
const test4 = `"hello"; world`;
const test5 = `"hello""world"`;

//should be true
console.log("test1",regex.test(test));
console.log("test2",regex.test(test2));

//should be false
console.log("test3",regex.test(test3));
console.log("test4",regex.test(test4));
console.log("test5",regex.test(test5));

【问题讨论】:

标签: javascript regex string comparison


【解决方案1】:

匹配前两种情况:

\"(.*)?\";?\s{1}\"(.*)?\";?

示例:

const regex = /\"(.*)?\";?\s{1}\"(.*)?\";?/;


const test = `"hello"; "world";`;
const test2 = `"hello"; "world"`;

const test3 = `"hello; "world"`;
const test4 = `"hello"; world`;
const test5 = `"hello""world"`;

//should be true
console.log("test1",regex.test(test));
console.log("test2",regex.test(test2));

//should be false
console.log("test3",regex.test(test3));
console.log("test4",regex.test(test4));
console.log("test5",regex.test(test5));

【讨论】:

    【解决方案2】:

    看来你想匹配分号分隔的字符串文字,这意味着你可以使用

    const sl = String.raw`"[^"\\]*(?:\\[^][^"\\]*)*"`;
    const regex = new RegExp(String.raw`^${sl}(?:\s*;\s*${sl})*;?$`);
    
    
    const test = `"hello"; "world";`;
    const test2 = `"hello"; "world"`;
    
    const test3 = `"hello; "world"`;
    const test4 = `"hello"; world`;
    const test5 = `"hello""world"`;
    
    //should be true
    console.log("test1",regex.test(test));
    console.log("test2",regex.test(test2));
    
    //should be false
    console.log("test3",regex.test(test3));
    console.log("test4",regex.test(test4));
    console.log("test5",regex.test(test5));

    正则表达式是^${sl}(?:\s*;\s*${sl})*;?$ 匹配

    • ^ - 字符串开头
    • ${sl} - 字符串文字模式
    • (?:\s*;\s*${sl})* - 零个或多个 ; 序列,其中包含零个或多个空格,然后是字符串文字模式
    • ;? - 一个可选的 ; 字符(如果您需要在后面匹配尾随空格,请添加 \s*
    • $ - 字符串结束。

    "[^"\\]*(?:\\[^][^"\\]*)*" 模式匹配

    • " - 一个 " 字符
    • [^"\\]* - 除了"\ 之外的零个或多个字符
    • (?:\\[^][^"\\]*)* - \ 出现零个或多个,然后是任何字符,然后是 "\ 以外的零个或多个字符
    • " - 一个 " 字符。

    【讨论】:

    • @CharlesLavalard ... Wiktor 的 模式在对字符串文字的描述方面更加精确/严格,从长远来看,template literal 方法更具适应性/可维护性,并且最高工艺。
    • 但是有一件事,像'"hiho"' 甚至'""' 这样的单个字符串文字通过了测试。 @CharlesLavalard ...边缘情况的要求是什么?
    【解决方案3】:
    /^"[^"]*"(?:\s*;\s*"[^"]*")+;?$/
      ---2---
             ---------3---------
                                -4
    

    ...可以分解成...

    1. /^ ... $/
    2. "[^"]*"
    3. (?:\s*;\s*"[^"]*")+
    4. ;?

    ... 然后转换为 ...

    1. 在新行开始和行结束之间...
    2. 匹配双引号,后跟可选的字符序列,每个字符不是双引号,后跟双引号。
    3. group ( ... ) 以下匹配但不捕获它 (?: ... ) 并强制此模式至少出现一次 (?: ... )+ ... 在此组中 ...
      • 匹配可选空格 (WS)、分号和另一个可选 WS 的序列,后跟 (2) 中已描述的模式。
    4. 允许整个匹配以无分号或仅以一个分号结尾。

    const regex = /^"[^"]*"(?:\s*;\s*"[^"]*")+;?$/;
    
    const test1 = `"hello"; "world";`;
    const test1a = `"hello" ; "world" ;"world"; "world"`;
    const test2 = `"hello" ; "world"`;
    const test2a = `"hello";"world" ; "world";"world"`;
    
    const test3 = `"hello; "world"`;
    const test4 = `"hello"; world`;
    const test5 = `"hello""world"`;
    
    
    const test6 = `"hello ; world;";"hello ;world"`;
    
    const test6a = `"hello ;world;" "hello; world"`;
    const test6b = `"hello; world"`;
    
    
    //should be true
    console.log("test1", regex.test(test1));
    console.log("test1a", regex.test(test1a));
    console.log("test2", regex.test(test2));
    console.log("test2a", regex.test(test2a));
    
    console.log("test6", regex.test(test6));
    
    //should be false
    console.log("test3", regex.test(test3));
    console.log("test4", regex.test(test4));
    console.log("test5", regex.test(test5));
    
    console.log("test6a", regex.test(test6a));
    console.log("test6b", regex.test(test6b));
    .as-console-wrapper { min-height: 100%!important; top: 0; }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-12
      • 1970-01-01
      • 2022-01-18
      相关资源
      最近更新 更多