字符串从给定的最后一个特定关键字

时间:2016-10-05 08:16:33

标签: javascript angularjs node.js

下面是一个完整的字符串,如何将字符串从“{”拆分为“}”意味着需要完整的对象。

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   234    0   159  100    75    584    275 --:--:-- --:--:-- --:--:--   584
{"scope":"Production","token_type":"bearer","expires_in":78,"refresh_token":"eregfgfg","access_token":"swewe23231c323"}

需要结果:

{"scope":"Production","token_type":"bearer","expires_in":78,"refresh_token":"eregfgfg","access_token":"swewe23231c323"}

3 个答案:

答案 0 :(得分:1)

试试这个

JSON.parse(yourobject) 

答案 1 :(得分:0)

这很简单......

var yourObj = yourStr.substring(yourStr.indexof('{'), yourstring.length-1);

var yourObj = yourStr.substring(yourStr.indexof('{'), yourStr.indexof('}'));

朋友这只有在您的字符串与您上面写的完全相同时才有效。

答案 2 :(得分:0)

使用正则表达式

var a  = '  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Curren Dload  Upload   Total   Spent    Left  Speed 100   234    0   159  100    75    584    275 --:--:-- --:--:-- --:--:--   584 {"scope":"Production","token_type":"bearer","expires_in":78,"refresh_token":"eregfgfg","access_token":"swewe23231c323"}';

console.log((/(\{.*\})/gmi).exec(a)[0]);

显示:“{”范围“:”生产“,”token_type“:”bearer“,”expires_in“:78,”refresh_token“:”eregfgfg“,”access_token“:”swewe23231c323“}”