The best I can suggest is a regex that matches the string as a whole. For the problem here is that a local parsing can produce different results from a global parsing.
My attempt at a solution would be:
^[^"]*(?:"(?:[^"\s]|[^"\s][^"]*[^"\s])?"[^"]*)*$
Example in Rubular. Explanation:
-
^
- start of string
-
[^"]*
- followed by zero or more non-quotationable characters (text outside the quotation marks)
-
(?:...)*
- followed by zero or more of:
-
"
- opens quotation marks
-
(?:...|...)?
- with or without:
-
[^"\s]
- a single character that is not quotation marks or spaces; or:
-
[^"\s]
- a character that is not quotation marks or spaces, followed by
-
[^"]*
- zero or more characters that are not double quotes, followed by
-
[^"\s]
- a character that is not quotation marks or spaces, followed by
-
"
- quoted quotes
-
[^"]*
- zero or more non-quotationable characters (text outside the quotation marks)
-
$
- end of string
Explaining in natural language, it takes an excerpt from the quotes, then an excerpt, an excerpt, an excerpt, and so on. The excerpts within quotation marks can be of three types: a) empty -
""
; b) with a single character -
"a"
; c) with a before and after character, and anything in the middle -
"a...b"
.
Note that everything this regex talks about is whether the string is valid or invalid: it can not tell you what character the error is in.
Update: If what you want is a regex that casts strings with error - and tells you where the error is - this was the best I could do: / p>
^[^"]*(?:"(?:[^"\s]|[^"\s][^"]*[^"\s])?"[^"]*)*("(?:\s[^"]*|[^"]*\s)")[^"]*(?:"(?:[^"\s]|[^"\s][^"]*[^"\s])?"[^"]*)*$
Example in jsFiddle . This "monstrosity" boils down to:
^ regex_original ("(?:\s[^"]*|[^"]*\s)") regex_original $
That is, "marry something that is correct, followed by something that is incorrect, followed by something that is correct." It will detect one and only one error of that type - if the string has two or more errors, or if it has a quotation mark that opens but does not close, etc., regex will not be able to get it. >
I think with a little more effort you can improve this a bit, but we are getting to the point where regex is no longer the most suitable tool for the job ...