最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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.

Share Improve this question asked Jun 3, 2015 at 8:57 Oam PsyOam Psy 8,66335 gold badges97 silver badges167 bronze badges
Add a ment  | 

7 Answers 7

Reset to default 4

Try:

$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);
发布评论

评论列表(0)

  1. 暂无评论