I'm using AngularJS in here. I have no problem matching those words except "C++". Every time I type in "c++" as keyword to generate the RegExp in Javascript and run the matching, I get the error in console as below:
SyntaxError: Invalid regular expression: /(\bc++\b)/: Nothing to repeat
The code snippet is as below:
$scope.data = [
{'title': 'Blue Java Programming Book'},
{'title': 'Red C++ Programming Book'},
{'title': 'Javascript Dummies Guide'}
];
$scope.submit = function() {
$scope.length = $scope.keywords.split(" ").length;
$scope.keywordsArray = $scope.keywords.split(" ");
$scope.pattern = "";
for (var y = 0; y < $scope.length; y++) {
$scope.pattern += "(?=.*?\\b" + $scope.keywordsArray[y] + "\\b)";
}
$scope.pattern+=".*";
$scope.patt = new RegExp($scope.pattern, "i");
for (var x = 0; x < $scope.data.length; x++) {
console.log("Match [" + x + "] " + $scope.patt.test($scope.data[x].description));
}
}
<input type="text" ng-model="keywords"></input>
<button ng-click="submit()">Submit</button>
I understand that the + sign in RegExp is for matching one or more times of the preceding character, then I tried hardcode the RegExp as below to test and it matches, but not the way I wanted as I need the RegExp to be generated every time I key in the keywords.
$scope.regExp = /c\+\++/i
Is there any way to generate a RegExp on the fly with multiple keywords to match an array of data that includes "c++"?
$scope.data?