thinkwei2012
V2EX  ›  问与答

请教大佬如何辨别字符串区别

  •  
  •   thinkwei2012 · May 26, 2020 · 1726 views
    This topic created in 2205 days ago, the information mentioned may be changed or developed.

    以 js 正则为例,如何区分 s1 和 s2 的不同? s1 是复制用户的,s2 是手打的 起初还以为是正则出了问题

    let s1 = '64165269‬B';
    let s2 = '64165269B';
    
    console.log(/^[0-9A-z]{8,11}$/.test(s1));
    console.log(/^[0-9A-z]{8,11}$/.test(s2));
    
    5 replies    2020-05-26 10:26:37 +08:00
    remarrexxar
        1
    remarrexxar  
       May 26, 2020
    console.log(charToUnicode(s1))
    \\ u0036 \\ u0034 \\ u0031 \\ u0036 \\ u0035 \\ u0032 \\ u0036 \\ u0039 \\ u202c \\ u0042
    console.log(charToUnicode(s2))
    \\ u0036 \\ u0034 \\ u0031 \\ u0036 \\ u0035 \\ u0032 \\ u0036 \\ u0039 \\ u0042

    多了不可见字符
    Vegetable
        2
    Vegetable  
       May 26, 2020
    如果你只是确定的话,可以用方向键移动光标看看有没有不可见字符。
    也可以保存两个文件用 diff 或者 git diff 之类工具。
    代码里最好 trim 一下,有时候复制会在前后添加莫名其妙的字符。
    thinkwei2012
        3
    thinkwei2012  
    OP
       May 26, 2020
    @remarrexxar 感谢,根据 \u202c 搜索了下,很有可能是用户从 excel 里直接复制粘贴的
    wysnylc
        4
    wysnylc  
       May 26, 2020
    lululau
        5
    lululau  
       May 26, 2020
    pbpaste | cat -v
    pbpaste | od -Ad -tc
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3014 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 51ms · UTC 15:13 · PVG 23:13 · LAX 08:13 · JFK 11:13
    ♥ Do have faith in what you're doing.