preg_match in JavaScript?

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

    get matched string back or false

    function preg_match (regex, str) {
      if (new RegExp(regex).test(str)){
        return regex.exec(str);
      }
      return false;
    }
    
    0 讨论(0)
  • 2020-12-08 13:58
    var text = 'price[5][68]';
    var regex = /price\[(\d+)\]\[(\d+)\]/gi;
    match = regex.exec(text);
    

    match[1] and match[2] will contain the numbers you're looking for.

    0 讨论(0)
提交回复
热议问题