javascript regex : only english letters allowed

前端 未结 3 564
青春惊慌失措
青春惊慌失措 2020-12-05 09:54

Quick question: I need to allow an input to only accept letters, from a to z and from A to Z, but can\'t find any expression for that. I want to use the javascript test() me

3条回答
  •  萌比男神i
    2020-12-05 10:26

    Another option is to use the case-insensitive flag i, then there's no need for the extra character range A-Z.

    var reg = /^[a-z]+$/i;
    console.log( reg.test("somethingELSE") ); //true
    console.log( "somethingELSE".match(reg)[0] ); //"somethingELSE"
    

    Here's a DEMO on how this regex works with test() and match().

提交回复
热议问题