I have a string like this,
我有一个像这样的字符串,
green open calldetails 1 0 4 0 10.7kb 10.7kb
green open stocksummary 1 0 3 0 8.6kb 8.6kb
I need to obtain Stocksummary and calldetails from this. This is what i have tried using regex,
我需要从中获取Stocksummary和calldetails。这是我尝试使用正则表达式,
var result = string.match(/(?:open )(.+)(?:1)/)[1];
Here is my full function:
这是我的全部功能:
routerApp.controller("elasticindex",function($scope,es){
es.cat.indices("b",function(r,q){
String St = string.match(/(?:open )(.+)(?:1)/)[1];
console.log(r,q);
});
});
Desired Output:
calldetails
stocksummary
1 个解决方案
#1
2
This non-greedy (lazy) regex should work instead:
这个非贪婪(懒惰)正则表达式应该工作:
/open +(.+?) +1/
RegEx Demo
var result = string.match(/open +(.+?) +1/)[1];
Or safe approach:
或安全的方法:
var result = (string.match(/open +(.+?) +1/) || ['', ''])[1];
Code:
var re = /open +(.+?) +1/g,
matches = [],
input = "green open calldetails 1 0 4 0 10.7kb 10.7kb green open stocksummary 1 0 3 0 8.6kb 8.6kb";
while (match = re.exec(input)) matches.push(match[1]);
console.log(matches);
JsFiddle Demo
#1
2
This non-greedy (lazy) regex should work instead:
这个非贪婪(懒惰)正则表达式应该工作:
/open +(.+?) +1/
RegEx Demo
var result = string.match(/open +(.+?) +1/)[1];
Or safe approach:
或安全的方法:
var result = (string.match(/open +(.+?) +1/) || ['', ''])[1];
Code:
var re = /open +(.+?) +1/g,
matches = [],
input = "green open calldetails 1 0 4 0 10.7kb 10.7kb green open stocksummary 1 0 3 0 8.6kb 8.6kb";
while (match = re.exec(input)) matches.push(match[1]);
console.log(matches);