Regex pattern for extraction of double-quoted and non-quoted strings Javascript

Viewed 42

im desperately trying to extract double quoted and non-quoted words from following string (Please note the single quotes around):

'"Cloud Technology" Foundation "Board"'

Desired groups are:

  1. "Cloud Technology"
  2. Foundation
  3. "Board"

I came up with this pattern (?:\"(.*?)\")|(?:\s(\S*?)\s) have a look in regex101.com

But the pattern doesnt works if the string would be for example '"Cloud Technology" Foundation'

2 Answers

What's about to add to second group charakter ' as additional possible end (or beginner):

(?:\"(.*?)\")|(?:[\s\'](\S*?)[\s\'])

You can use

console.log(`'"Cloud Technology" Foundation "Board"'`.match(/"[^"]+"|[^\s']+/g))

See the regex demo. Details:

  • "[^"]+" - ", zero or more chars other than " and then a " char
  • | - or
  • [^\s']+ - one or more chars other than whitespace and ' chars.
Related