regex - Angular is returning "TypeError: Cannot read property" when using regular expressions -
i trying extract youtube id form input provided. output fine receiving error typeerror: cannot read property '2' of null
$scope.message.color = 'green'; $scope.message.content = 'sss'; $scope.videos.youtubeadd=true; $scope.$watch('item.url', function() { var re = /(\?v=|\/\d\/|\/embed\/|\/v\/|\.be\/)([a-za-z0-9\-\_]+)/; value = $scope.item.url; value = value.match(re)[2]; $scope.item.url = value; });
please see here http://jsfiddle.net/k39du/ should helps. can check if matches before take second element array
var app = angular.module('app', []);
app.controller('myctrl', function ($scope) { $scope.url = 'https://www.youtube.com/watch?v=i38q5ad5gam'; var re = /(\?v=|\/\d\/|\/embed\/|\/v\/|\.be\/)([a-za-z0-9\-\_]+)/; value = $scope.url; $scope.arrays = value.match(re); if ($scope.arrays && $scope.arrays.length > 1) { $scope.id = $scope.arrays[2]; } });
Comments
Post a Comment