Javascript 如何修复正则表达式,使其仅从活动中选择基本出价,而不跳过我的结束字符串基本出价文本

Javascript 如何修复正则表达式,使其仅从活动中选择基本出价,而不跳过我的结束字符串基本出价文本,javascript,regex,Javascript,Regex,我的正则表达式抓住了这个: campaign":{"id":100134,"state":"active","code":null,"advertiser_id":21147,"line_item_id":44854,"creative_id":481909,"pixel_id":null,"short_name":null,"name":"WSPA ROE 0711","profile_id":438747,"start_date":"2011-07-06 00:00:00","end_dat

我的正则表达式抓住了这个:

campaign":{"id":100134,"state":"active","code":null,"advertiser_id":21147,"line_item_id":44854,"creative_id":481909,"pixel_id":null,"short_name":null,"name":"WSPA ROE 0711","profile_id":438747,"start_date":"2011-07-06 00:00:00","end_date":null,"timezone":"Europe/London","priority":5,"cadence_modifier_enabled":true,"cpc_goal":null,"cpm_bid_type":"base","base_bid":0.16,"min_bid":null,"max_bid":null,"bid_margin":0,"roadblock_creatives":false,"roadblock_type":"no_roadblock","inventory_type":"real_time","last_modified":"2014-05-28 16:06:01","max_learn_bid":null,"cadence_type":"creative","click_url":null,"require_cookie_for_tracking":true}}}{"response":{"status":"OK","count":1,"start_element":0,"num_elements":100,"campaign":{"id":12039447,"state":"active","code":null,"advertiser_id":180353,"line_item_id":2703939,"creative_id":null,"pixel_id":null,"short_name":null,"name":"Copy Jan Programmatic BAU - 6 RTG - Register 2a. (7D)","profile_id":51061721,"start_date":"2015-08-20 00:00:00","end_date":null,"timezone":"Europe/London","priority":5,"cadence_modifier_enabled":false,"cpc_goal":null,"cpm_bid_type":"base","base_bid":1.25,|100134
当我想抓住它时:

campaign":{"id":100134,"state":"active","code":null,"advertiser_id":21147,"line_item_id":44854,"creative_id":481909,"pixel_id":null,"short_name":null,"name":"WSPA ROE 0711","profile_id":438747,"start_date":"2011-07-06 00:00:00","end_date":null,"timezone":"Europe/London","priority":5,"cadence_modifier_enabled":true,"cpc_goal":null,"cpm_bid_type":"base","base_bid":0.16,

两批。因此,请打印:

100134|0.16
12039447|1.25
我怎样才能解决这个问题

这是我的文本javascript

</head>
<body>

<script>
  var theString = '{"response":{"status":"OK","count":1,"start_element":0,"num_elements":100,"campaign":{"id":100134,"state":"active","code":null,"advertiser_id":21147,"line_item_id":44854,"creative_id":481909,"pixel_id":null,"short_name":null,"name":"WSPA ROE 0711","profile_id":438747,"start_date":"2011-07-06 00:00:00","end_date":null,"timezone":"Europe/London","priority":5,"cadence_modifier_enabled":true,"cpc_goal":null,"cpm_bid_type":"base","base_bid":0.16,"min_bid":null,"max_bid":null,"bid_margin":0,"roadblock_creatives":false,"roadblock_type":"no_roadblock","inventory_type":"real_time","last_modified":"2014-05-28 16:06:01","max_learn_bid":null,"cadence_type":"creative","click_url":null,"require_cookie_for_tracking":true}}}{"response":{"status":"OK","count":1,"start_element":0,"num_elements":100,"campaign":{"id":12039447,"state":"active","code":null,"advertiser_id":180353,"line_item_id":2703939,"creative_id":null,"pixel_id":null,"short_name":null,"name":"Copy Jan Programmatic BAU - 6 RTG - Register 2a. (7D)","profile_id":51061721,"start_date":"2015-08-20 00:00:00","end_date":null,"timezone":"Europe/London","priority":5,"cadence_modifier_enabled":false,"cpc_goal":null,"cpm_bid_type":"base","base_bid":1.25,"min_bid":null,"max_bid":null,"bid_margin":0,"roadblock_creatives":false,"roadblock_type":"no_roadblock","inventory_type":"real_time","last_modified":"2016-02-09 12:11:02","max_learn_bid":null,"cadence_type":"advertiser","click_url":null,"require_cookie_for_tracking":true}}}';

var regexp = /campaign":{"id":([0-9]+),"state":.*"base_bid":([0-9.]+),/g;

var match = regexp.exec(theString);
while (match != null){
    console.log(match[0] + '|' + match[1]);
    var match = regexp.exec(theString);
}

</script>
</body>
<html>

var theString={“response”:{“status”:“OK”,“count”:1,“start_元素”:0,“num_元素”:100,“campaign”:{“id”:100134,“state”:“active”,“code”:null,“advertiser_id”:21147,“line_item_id”:44854,“creative_id”:481909,“pixel_id”:null,“short_name”:null,“name”:“WSPA ROE 0711”,“profile_id”:438747,“start_date”:“2011-07-06 00:00”,“end_date”:null,“时区”:“欧洲/伦敦”,“优先级”:5,“cadence\u修改器启用”:true,“cpc\u目标”:null,“cpm\u投标类型”:“基本”,“基本投标”:0.16,“最低投标”:null,“最高投标”:null,“投标保证金”:0,“路障\u创意”:false,“路障类型”:“无路障”,“库存类型”:“实时”,“上次修改”:“2014-05-28 16:06:01”,“最大学习投标”:null,“路障\u类型”:“创意”,“点击url”“:null,“需要cookie用于跟踪”:true}}{”response:{“状态”:“确定”,“计数”:1,“开始元素”:0,“数量元素”:100,“活动”:{“id”:12039447,“状态”:“活动”,“代码”:null,“广告商id”:180353,“行项目id”:2703939,“创意项目id”:null,“像素id”:null,“短名称”:Jan:“名称”:“复制编程BAU-6 RTG-寄存器2a.(7D)”,“配置文件id”:51061721,“开始日期”:“2015-08-20 00:00:00”,“结束日期”:null,“时区”:“欧洲/伦敦”,“优先级”:5,“节奏调整器启用”:false,“cpc目标”:null,“cpm投标类型”:“基本”,“基本投标”:1.25,“最低投标”:null,“最高投标”:null,“投标保证金”:0,“路障创意”:false,“路障类型”:“无路障”,“库存类型”:“实时”,“上次修改”:“2016-02-09 12:11:02”,“max_learn_bid”:null,“cadence_type”:“广告商”,“点击url”:null,“需要cookie_跟踪”:true}}}};
var regexp=/campaign:{“id”:([0-9]+),“state”:。*“base_bid”:([0-9.]+),/g;
var match=regexp.exec(字符串);
while(匹配!=null){
log(匹配[0]+'|'+匹配[1]);
var match=regexp.exec(字符串);
}

我认为正则表达式的问题在于“base\u bid”之前的
*
匹配了第一个“id”和最后一个“base\u bid”之间的所有内容。我不确定如何修复正则表达式

但是,您是否使用过
JSON.parse
?它内置于Javascript中,将有效的JSON字符串转换为Javascript对象。如果您能够将
jsonString
拆分为单独的响应,您可以非常简单地获得活动ID和基本出价值

这不是一个完美的解决方案,但我将尝试在这里说明我的想法:

// Your original string.
var jsonString = '{"response":{"status":"OK","count":1,"start_element":0,"num_elements":100,"campaign":{"id":100134,"state":"active","code":null,"advertiser_id":21147,"line_item_id":44854,"creative_id":481909,"pixel_id":null,"short_name":null,"name":"WSPA ROE 0711","profile_id":438747,"start_date":"2011-07-06 00:00:00","end_date":null,"timezone":"Europe/London","priority":5,"cadence_modifier_enabled":true,"cpc_goal":null,"cpm_bid_type":"base","base_bid":0.16,"min_bid":null,"max_bid":null,"bid_margin":0,"roadblock_creatives":false,"roadblock_type":"no_roadblock","inventory_type":"real_time","last_modified":"2014-05-28 16:06:01","max_learn_bid":null,"cadence_type":"creative","click_url":null,"require_cookie_for_tracking":true}}}{"response":{"status":"OK","count":1,"start_element":0,"num_elements":100,"campaign":{"id":12039447,"state":"active","code":null,"advertiser_id":180353,"line_item_id":2703939,"creative_id":null,"pixel_id":null,"short_name":null,"name":"Copy Jan Programmatic BAU - 6 RTG - Register 2a. (7D)","profile_id":51061721,"start_date":"2015-08-20 00:00:00","end_date":null,"timezone":"Europe/London","priority":5,"cadence_modifier_enabled":false,"cpc_goal":null,"cpm_bid_type":"base","base_bid":1.25,"min_bid":null,"max_bid":null,"bid_margin":0,"roadblock_creatives":false,"roadblock_type":"no_roadblock","inventory_type":"real_time","last_modified":"2016-02-09 12:11:02","max_learn_bid":null,"cadence_type":"advertiser","click_url":null,"require_cookie_for_tracking":true}}}';

// Find where the first response ends and the second begins.
// (+ 1 because we need to get between the }{ when we split.)
// (It would be great if the strings weren't concatenated to begin with.)
var splitIndex = jsonString.indexOf('}{') + 1;
// Get the two individual JSON responses.
var firstResponseJson = jsonString.substring(0, splitIndex);
var secondResponseJson = jsonString.substring(splitIndex);

// Parse the first JSON string.
var firstResponse = JSON.parse(firstResponseJson);
// We now have an Object with a "response" attribute, which contains
// all the information we need about this campaign.
var firstId = firstResponse.response.campaign.id;
var firstBaseBid = firstResponse.response.campaign.base_bid;
console.log(firstId + '|' + firstBaseBid);
很抱歉没有使用正则表达式给出答案,但我认为如果您能够将JSON解析为一个对象,它最终将是一个更灵活的解决方案

// Your original string.
var jsonString = '{"response":{"status":"OK","count":1,"start_element":0,"num_elements":100,"campaign":{"id":100134,"state":"active","code":null,"advertiser_id":21147,"line_item_id":44854,"creative_id":481909,"pixel_id":null,"short_name":null,"name":"WSPA ROE 0711","profile_id":438747,"start_date":"2011-07-06 00:00:00","end_date":null,"timezone":"Europe/London","priority":5,"cadence_modifier_enabled":true,"cpc_goal":null,"cpm_bid_type":"base","base_bid":0.16,"min_bid":null,"max_bid":null,"bid_margin":0,"roadblock_creatives":false,"roadblock_type":"no_roadblock","inventory_type":"real_time","last_modified":"2014-05-28 16:06:01","max_learn_bid":null,"cadence_type":"creative","click_url":null,"require_cookie_for_tracking":true}}}{"response":{"status":"OK","count":1,"start_element":0,"num_elements":100,"campaign":{"id":12039447,"state":"active","code":null,"advertiser_id":180353,"line_item_id":2703939,"creative_id":null,"pixel_id":null,"short_name":null,"name":"Copy Jan Programmatic BAU - 6 RTG - Register 2a. (7D)","profile_id":51061721,"start_date":"2015-08-20 00:00:00","end_date":null,"timezone":"Europe/London","priority":5,"cadence_modifier_enabled":false,"cpc_goal":null,"cpm_bid_type":"base","base_bid":1.25,"min_bid":null,"max_bid":null,"bid_margin":0,"roadblock_creatives":false,"roadblock_type":"no_roadblock","inventory_type":"real_time","last_modified":"2016-02-09 12:11:02","max_learn_bid":null,"cadence_type":"advertiser","click_url":null,"require_cookie_for_tracking":true}}}';

// Find where the first response ends and the second begins.
// (+ 1 because we need to get between the }{ when we split.)
// (It would be great if the strings weren't concatenated to begin with.)
var splitIndex = jsonString.indexOf('}{') + 1;
// Get the two individual JSON responses.
var firstResponseJson = jsonString.substring(0, splitIndex);
var secondResponseJson = jsonString.substring(splitIndex);

// Parse the first JSON string.
var firstResponse = JSON.parse(firstResponseJson);
// We now have an Object with a "response" attribute, which contains
// all the information we need about this campaign.
var firstId = firstResponse.response.campaign.id;
var firstBaseBid = firstResponse.response.campaign.base_bid;
console.log(firstId + '|' + firstBaseBid);