Vidura Dantanarayana
Vidura Dantanarayana

Reputation: 531

Regex to find a value after a specific word with Google App Script

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.

Upvotes: 2

Views: 1195

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626738

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

Upvotes: 1

Related Questions