0

I'm currently trying to validate strings in a text box with this code:

 $scope.regexArray = ["Alarm id (?<serverNumber>\d+) has been received from video server number (?<alarmNumber>\d+).", "Alarm id (?<serverNumber>\d+) has been received from video server number (?<alarmNumber>\d+)."];

    $scope.validTextBox = false; 
    $scope.userInput = "";
    $scope.validateExpression = function () {

        angular.forEach($scope.regexArray, function (value, key) {

            var test = $scope.userInput.match(value);

            console.log(test);
        })
    }

The output of test is this:

["Alarm id 4 has been received from video server number 4", index: 0, input: "Alarm id 4 has been received from video server number 4", groups: undefined]

0: "Alarm id 4 has been received from video server number 4"
groups: undefined
index: 0
input: "Alarm id 4 has been received from video server number 4"
length: 1
__proto__: Array(0)

I can’t seem to access any capture groups. Trying test[0] doesn’t work.

Rory O'Kane
  • 29,210
  • 11
  • 96
  • 131
  • 3
    You have no capturing groups in the pattern. A capturing group is a pair of unescaped parentheses. Like `"Alarm id (\\d+) has been received from video server number (\\d+)"` – Wiktor Stribiżew Apr 09 '18 at 21:19
  • I think the problem with your patterns is that you are using **named capturing groups** which is experimental and might not be supported in your browser. The solution to this is using only **numbered capturing groups**. – Rodrigo Ferreira Apr 09 '18 at 21:46
  • A brief explanation of this issue can be found [here](https://stackoverflow.com/a/5367407/8371135) – Rodrigo Ferreira Apr 09 '18 at 21:47

1 Answers1

1

Changes to make:

  • (?<serverNumber>\d+) is not valid JavaScript regexp syntax for a capturing group. JavaScript only understands anonymous capture groups such as (\d+). As demonstrated further down, you can give names to matches by storing them in variables after extracting them from the match array with [].

  • It's not necessary to make the code work in this case, but your code will be clearer if you change your regexArray to contain actual regexes /…/ instead of strings "…" that will be converted to regexes by .match.

The following code demonstrates successful matches:

var regexArray = [
  /Alarm id (\d+) has been received from video server number (\d+)./,
  /Alarm id (\d+) has been received from video server number (\d+)./
];

var userInput = "Alarm id 1000 has been received from video server number 6.";

regexArray.forEach(function(value) {
  var matches = userInput.match(value);
  console.log(matches);
});

To get specific matches from the matches array, you can add this code:

var alarmId = matches[1];
var serverId = matches[2];

Note that alarmId and serverId will be strings after the above. If you want to convert them to numbers, so that the extracted strings "012" and "12" would be treated the same, add parseInt:

var alarmNumber = parseInt(matches[1], 10);
var serverNumber = parseInt(matches[2], 10);
Rory O'Kane
  • 29,210
  • 11
  • 96
  • 131