Check whether a string matches a regex in JS

后端 未结 11 2172
余生分开走
余生分开走 2020-11-22 12:02

I want to use JavaScript (can be with jQuery) to do some client-side validation to check whether a string matches the regex:

^([a-z0-9]{5,})$
11条回答
  •  迷失自我
    2020-11-22 12:26

    I would recommend using the execute method which returns null if no match exists otherwise it returns a helpful object.

    let case1 = /^([a-z0-9]{5,})$/.exec("abc1");
    console.log(case1); //null
    
    let case2 = /^([a-z0-9]{5,})$/.exec("pass3434");
    console.log(case2); // ['pass3434', 'pass3434', index:0, input:'pass3434', groups: undefined]
    

提交回复
热议问题