javascript - Removing brackets around a string and placing contents into new object -


i have angular service returns array number of objects inside.

$scope.data:

[     {         date: "03/12/2014",         name: "mr blue",         title: "math teacher (germany)"     },     {         date: "04/02/2015",         name: "mrs yellow",         title: "chemistry teacher (spain)"     }, ] 

you can see title field contains title , location. how can separate title , location? whilst removing brackets too?

service:

$scope.loadfeed=function(e){             myservice.parsefeed(url).then(function(res) {         $scope.data = res.data.responsedata.feed.entries;     }); } 

what have tried is:

$scope.loadfeed=function(e){             myservice.parsefeed(url).then(function(res) {         $scope.data = res.data.responsedata.feed.entries;          var strwithoutbracket = $scope.data[0].title.replace(/\(.*?\)/g,'');         console.log(strwithoutbracket);          $scope.location = strwithoutbracket;      }); } 

however console.log(strwithoutbracket); displaying as:

chemistry teacher 

essentially after $scope.title without location. , $scope.location without title.

here complete solution title , location :

var str = "chemistry teacher (spain)";  var regexp = /\(([^)]+)\)/; var matches = regexp.exec(str);  var title = str.substring(0, str.indexof('(')); var location = matches[1];  console.log('title : ' + title); console.log('location : ' + location); 

jsbin here


Comments