preg_match in JavaScript?

后端 未结 8 1954
逝去的感伤
逝去的感伤 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:42

    var thisRegex = new RegExp('\[(\d+)\]\[(\d+)\]');
    
    if(!thisRegex.test(text)){
        alert('fail');
    }
    

    I found test to act more preg_match as it provides a Boolean return. However you do have to declare a RegExp var.

    TIP: RegExp adds it's own / at the start and finish, so don't pass them.

提交回复
热议问题