I have an angular service which returns an array with a 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 from the title field it contains a title and a location. How can i separate the title and location? Whilst removing the brackets too?
Service:
$scope.loadFeed=function(e){
myService.parseFeed(url).then(function(res) {
$scope.data = res.data.responseData.feed.entries;
});
}
What i 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);
is displaying as:
chemistry teacher
Essentially what i am after is a $scope.title
without the location. And $scope.location
without the title.
I have an angular service which returns an array with a 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 from the title field it contains a title and a location. How can i separate the title and location? Whilst removing the brackets too?
Service:
$scope.loadFeed=function(e){
myService.parseFeed(url).then(function(res) {
$scope.data = res.data.responseData.feed.entries;
});
}
What i 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);
is displaying as:
chemistry teacher
Essentially what i am after is a $scope.title
without the location. And $scope.location
without the title.
7 Answers
Reset to default 4Try:
$scope.data = [
{
date: "03/12/2014",
name: "mr blue",
title: "math teacher (Germany)"
},
{
date: "04/02/2015",
name: "mrs yellow",
title: "chemistry teacher (Spain)"
},
];
angular.forEach($scope.data, function(item){
var values = /(.*)\s+\((.+)\)\s*$/.exec(item.title||"") || [];
item.title = values[1];
item.location = values[2];
});
console.log($scope.data);
Demo
Here is a plete solution for title and 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
You are already getting chemistry teacher, which you should set to title instead of location.
Here is what you can do:
var regExp = /\(([^)]+)\)/;
$scope.location = regExp.exec($scope.data[0].title);
$scope.data[0].title = $scope.data[0].title.replace(/\(.*?\)/g,'');
Should have both your title and location updated as required.
Here's what you can try. In the regex below I assumed that at least one whitespace character between title and location in brackets.
var locationRegex = /\s+\(([a-zA-Z]*)\)*/;
var strWithoutBracket = $scope.data[0].title.replace(locationRegex,'');
var location = $scope.data[0].title.match(locationRegex)[1];
You can use code like this:
var str = "math teacher (Germany)";
var m = str.match(/(.*) *\((.*)\)/);
var obj = {
title: m[1],
location: m[2]
};
document.getElementById('output').innerHTML = JSON.stringify(obj);
<div id="output"></div>
Try this
var strWithoutBracket = $scope.data[0].title.replace(/([()])+/g,'');
try this one
var strWithoutBracket = $scope.data[0].title.split((/\(([^}]+)\)/)[1]));
console.log(strWithoutBracket);