qwertymk
qwertymk

Reputation: 35344

Regex match between two quotes

Here are the types of strings I want to match

(string are single quotes delimited (think var str = '"hi, what"s up, what a nice day"';)):

  1. ' "This is a str"'
  2. ' "This is also a str "
  3. '"So is this'
  4. '"This should "also match un"til th"e last "'

In each case it should capture whatever is after the first dquote if there's only spaces before it up to a last quote (which is completely optional).

Another thing, a string containing just " shouldn't match ex '""""""""""""' shouldn't match at all.

Also leading and trailing whitespaces are ignored both between the quotes and outside the quotes.

Here's the regex I have so far:

/^\s*\"\s*(.*?)\s*(?:\"\s*)?$

But it also gets '""""""""""""""""' so that's where I'm stuck.

What can I do to not match a whole string of just dquotes?

EDIT:

I think I explained what I want wrong, I want it to match a string of dquotes but not capture any of it

Upvotes: 1

Views: 1700

Answers (1)

Jan Pöschko
Jan Pöschko

Reputation: 5580

You can alter your regex so that the content contains at least one non-" character:

/^\s*\"\s*(.*?[^"].*?)\s*(?:\"\s*)?$/

Update: If you don't want starting and trailing " to be captured, just allow several of them at the borders:

/^\s*\"+\s*(.*?)\s*(?:\"+\s*)?$/

Upvotes: 1

Related Questions