V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
dieeman
V2EX  ›  程序员

求一个正则表达式啊

  •  
  •   dieeman · 2020-11-12 15:20:27 +08:00 · 817 次点击
    这是一个创建于 1252 天前的主题,其中的信息可能已经有所发展或是发生改变。
    比如有一个字符串 abc[de..f]kkk[safs]sasdf[fisadfi..]5445
    我想要输出[de..f],[fisadfi..]
    我的想法是先匹配中括号,在匹配中括号里的..得到最终的结果
    奈何水平太低,求各位大佬帮忙看看
    dieeman
        1
    dieeman  
    OP
       2020-11-12 15:23:26 +08:00
    求解
    adajoy
        2
    adajoy  
       2020-11-12 16:18:55 +08:00   ❤️ 1
    re = /\[[^\]]*?\.\..*?\]/g
    > /\[[^\]]*?\.\..*?\]/g

    re.exec(s)
    > ["[de..f]", index: 3, input: "abc[de..f]kkk[safs]sasdf[fisadfi..]5445", groups: undefined]

    re.exec(s)
    > ["[fisadfi..]", index: 24, input: "abc[de..f]kkk[safs]sasdf[fisadfi..]5445", groups: undefined]

    re.exec(s)
    > null

    这样吗
    dieeman
        3
    dieeman  
    OP
       2020-11-12 16:52:43 +08:00
    @adajoy 差不多就是这样 感谢
    dieeman
        4
    dieeman  
    OP
       2020-11-12 17:05:12 +08:00
    @adajoy abc[[sdfas]de..f]kkk[safs]sasdf[fisadfi..]5445 这种内部有嵌套的好像不行
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3109 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 00:18 · PVG 08:18 · LAX 17:18 · JFK 20:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.