preg_match in JavaScript?

后端 未结 8 1951
逝去的感伤
逝去的感伤 2020-12-08 13:17

Is it possible in JavaScript to do something like preg_match does in PHP ?

I would like to be able to get two numbers from str

8条回答
  •  心在旅途
    2020-12-08 13:41

    Some Googling brought me to this :

    function preg_match (regex, str) {
      return (new RegExp(regex).test(str))
    }
    console.log(preg_match("^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}$","test"))
    console.log(preg_match("^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}$","what@google.com"))

    See https://locutus.io for more info.

提交回复
热议问题