2

I have a string and I wrote following code to extract "NewUsers19" from that.

function getNoOfUsers() {

  var noSpace = "UserRejectedOrderCount25NewUsers19FundedNewUsers14DepositAmount($)43165.23DepositCount75WithdrawAmount($)53510";
  var regex = /(?:NewUsers)([0-9]+)/;
  var value = regex.exec(noSpace);
  return value;
  
}

The regex worked fine in online editors but I don't know why it does not work in Google App Script. Any help will be appreciated.

pnuts
  • 58,317
  • 11
  • 87
  • 139

1 Answers1

1

You should remove the non-capturing group and then return value[1]:

function getNoOfUsers() {
  var noSpace = "UserRejectedOrderCount25NewUsers19FundedNewUsers14DepositAmount($)43165.23DepositCount75WithdrawAmount($)53510";
  var regex = /NewUsers([0-9]+)/;
  var value = regex.exec(noSpace);
  return value ? value[1] : "";
}

Tested in Google Docs:

enter image description here

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563