Simpler regular expression for parsing quoted strings

The question is simple. I have a string containing several elements that are embedded in quotation marks:

var str = "'alice'   'anna marie' 'benjamin' 'christin'     'david' 'muhammad ali'"

And I want to parse it so that I have all these names in an array:

result = [
 'alice',
 'anna marie',
 'benjamin',
 'christin',
 'david',
 'muhammad ali'
]

I am currently using this code to do the job:

var result = str.match(/\s*'(.*?)'\s*'(.*?)'\s*'(.*?)'\s*'(.*?)'/);

But this regex is too long and not flexible, so if I have more elements in the string str, I need to edit the regex.

What is the fastest and most effective way to do this parsing? Performance and slippiness are important in our web application.

I considered the following question, but this is not my answer:

+5
4

g.

var matches = str.match(/'[^']*'/g);

, - REGEX, JavaScript g. ( ) , :

if (matches)
    for (var i=0, len=matches.length; i<len; i++)
        matches[i] = matches[i].replace(/'/g, '');

[EDIT] - , split() , , ( - ) .

+8

global, , . , , :

var buf = "'abc' 'def' 'ghi'";
var exp = /'(.*?)'/g;
for(var match=exp.exec(buf); match!=null; match=exp.exec(buf)) {
  alert(match[0]);
}

, .

EDIT: /'(.*?)'/g ('), *? .

+1

I came here with an approach that could parse the string for quotes and quotes, keep the order of quotes and not quotes, and then output them using specific tags wrapped around them for React or React Native, so I didn't use the answers here, because that I was not sure how to make them fit my needs, and then did it.

function parseQuotes(str) {
  var openQuote = false;
  var parsed = [];
  var quote = '';
  var text = '';
  var openQuote = false;

  for (var i = 0; i < str.length; i++) {
    var item = str[i];
    if (item === '"' && !openQuote) {
      openQuote = true;
      parsed.push({ type: 'text', value: text });
      text = '';
    }
    else if (item === '"' && openQuote) {
      openQuote = false;
      parsed.push({ type: 'quote', value: quote });
      quote = '';
    }
    else if (openQuote) quote += item;
    else text += item;  
  }

  if (openQuote) parsed.push({ type: 'text', value: '"' + quote });
  else parsed.push({ type: 'text', value: text });

  return parsed;
}

What about this:

'Testing this "shhhh" if it "works!" " hahahah!'

produces the following:

[
  {
    "type": "text",
    "value": "Testing this "
  },
  {
    "type": "quote",
    "value": "shhhh"
  },
  {
    "type": "text",
    "value": " if it "
  },
  {
    "type": "quote",
    "value": "works!"
  },
  {
    "type": "text",
    "value": " "
  },
  {
    "type": "text",
    "value": "\" hahahah!"
  }
]

which makes it easy to wrap tags around it depending on what it is.

https://jsfiddle.net/o6seau4e/4/

+1
source

One way:

var str = "'alice' 'benjamin' 'christin' 'david'";
var result = {};

str.replace(/'([^']*)'/g, function(m, p1) {
    result[p1] = "";
});

for (var k in result) {
    alert(k);
}
0
source

All Articles