thinkwei2012
0.02D
V2EX  ›  问与答

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

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