Check whether a string matches a regex in JS

后端 未结 11 2179
余生分开走
余生分开走 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:10

    const regExpStr = "^([a-z0-9]{5,})$"
    const result = new RegExp(regExpStr, 'g').test("Your string") // here I have used 'g' which means global search
    console.log(result) // true if it matched, false if it doesn't

提交回复
热议问题