【问题标题】:How to make a regex that makes only letters and numbers are allowed, with no numbers at the beginning or the end如何制作一个只允许字母和数字的正则表达式,开头或结尾都没有数字
【发布时间】:2022-12-10 19:13:42
【问题描述】:

我想制作一个正则表达式: ahmed0saber 有效 0ahmedsaber 无效 ahmedsaber0 无效 ahmed_saber 无效

我试过这个 [a-z0-9_] 但它没有按预期工作!

【问题讨论】:

    标签: javascript html css regex regex-lookarounds


    【解决方案1】:

    这应该工作^[a-z][a-zd]*[a-z]$

    const regex = /^[a-z][a-zd]*[a-z]$/
    const tests = ['ahmed0saber', '0ahmedsaber', 'ahmedsaber0', 'ahmed_saber']
    tests.forEach(test)
    
    function test(name) {
      console.log(name, name.match(regex) ? 'yes' : 'no')
    }

    【讨论】:

      猜你喜欢
      • 2011-05-19
      • 1970-01-01
      • 2011-12-29
      • 2017-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多