Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Regex matchAll function not working

whenever I run the code below I get en error TypeError: responseData.matchAll is not a function

    var responseData = response.data.toString(); 
    var regex = new RegExp('(<dbname>)(.*?)(?=<\/dbname>)', 'g'); 

    var matches = responseData.matchAll(regex);
    

When I replace matchAll with exec it works! However, I need to use matchAll. This is driving me crazy. Thanks

like image 436
Paul Trimor Avatar asked Aug 06 '26 13:08

Paul Trimor


1 Answers

If you need matchAll, use it if supported:

var responseData = "<dbname>hhh</dbname>hhh<dbname>hhh3</dbname>"; 
var regex = new RegExp('<dbname>(.*?)(?=</dbname>)', 'g'); 
console.log(Array.from(responseData.matchAll(regex), x=>x[1]));
// => ["hhh","hhh3"]
   

You can also use exec:

var responseData = "<dbname>hhh</dbname>hhh<dbname>hhh3</dbname>"; 
var regex = new RegExp('<dbname>(.*?)(?=</dbname>)', 'g'); 
while(match=regex.exec(responseData)){
  console.log(match[1]);
}
like image 159
Ryszard Czech Avatar answered Aug 08 '26 03:08

Ryszard Czech



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!