1

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,

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 Answer 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

Sign up to request clarification or add additional context in comments.

3 Comments

it gives open stocksummary 1
Problem was g flag, it is fixed now. Check my updated version.
it works fine, i am having a small issue. since it is a string, "green open calldetails 1 0 4 0 10.7kb 10.7kb green open stocksummary 1 0 3 0 8.6kb 8.6kb" how can i get indexes individualy. how can i get calldetails and stocksummary?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.