Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Any RegEx experts out there?

Any RegEx experts out there?

Any RegEx experts out there?


First off apologies if this question is in the wrong place... There is no dedicated RegEx forum and since I'm working in JS and in my experience RegEx is most commonly used in JS - here lies my problem :)

I have a number of search strings that I would like to split into an array. The search is usually separated by spaces, but the search terms can also contain spaces and in this case they are quoted. Here's an example:

SearchTerm1=foo SearchTerm2=bar SearchTerm3="hello world" SearchTerm4="nospacesbutquotedanyway" SearchTerm5=foo,bar

I would like to return as an array:
[2][1]hello world

I'm toying with writing a function that first searches the string for quoted chunks, perhaps replacing the spaces inside quotes with an uncommon character in order to split the string by space and replace them back again afterwards. But hoping someone might come up with a magic RegEx to neatly do the job for me!

Thanks in advance,


RE: Any RegEx experts out there?


Something like this ?

CODE --> Console

>  s = 'SearchTerm1=foo SearchTerm2=bar SearchTerm3="hello world" SearchTerm4="nospacesbutquotedanyway" SearchTerm5=foo,bar'
   "SearchTerm1=foo SearchTerm2=bar SearchTerm3="hello world" SearchTerm4="nospacesbutquotedanyway" SearchTerm5=foo,bar"
>  result = []; s.replace(/(\S+)=(?:(["'])(.*?)\2|(\S*))/g, function(g0, g1, g2, g3, g4) { result.push([g1, g3 || g4]) })
   "undefined undefined undefined undefined undefined"
>  result
   [["SearchTerm1", "foo"], ["SearchTerm2", "bar"], ["SearchTerm3", "hello world"], ["SearchTerm4", "nospacesbutquotedanyway"], ["SearchTerm5", "foo,bar"]] 
Note that if you need escaping, like SearchTerm0="John \"Hannibal\" Smith", that will be harder to solve with regular expression.


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close