- Get the full quotes. This is done using
String.split()
.
- (Optional) remove
""
(double quotes) of the quotes. This can be done with String.replace()
inside Array.map()
.
1. Getting the quotes
Using the following Regular Expression will split the String at the white-spaces between ""
(double quotes), however will not consider \"
(escaped double quote): /(?<=[^\\]")\s+(?=")/
Here an explanation of it:
(?<=[^\\]") # Is preceded by not a backslash and a double quote
# Better said: is preceded by an un-escaped double quote
\s+ # Consists of at least one, and only white-spaces
(?=") # Is followed by double quote
If there is none, or any other character than white-spaces, it will not be split. This allows for double quotes with beginning or trailing white-spaces (e.g. " This "
), but not for double quotes only consisting of white-spaces.
var message = '"Containing \" escaped quote" "Trailing white-spaces here " " Beginning and trailing white-spaces here " " Beginning white-spaces here"';
console.log(message.split(/(?<=[^\\]")\s+(?=")/));
Why check on both sides? Wouldn't it suffice to only look ahead?
This becomes a problem for when the input string has a quoted part with trailing white-spaces before another quoted part, e.g. "Trailing " "Regular"
.
Here, the first split will be right after "Trailing
, splitting away
(white-space) before its "
(double quote). The next split will be at the third "
(double quote), splitting away its
(white-space). However, the end-double quote "
of "Trailing "
will now be part of the returned array, since on both its sides are splits.
Also, without checking if the first quote is escaped, escaped quotes are impossible.
Looking both ahead and before solves this problem. However, it still doesn't solve the problem of " "
(double quotes containing white-spaces) being excluded. To my knowledge, splitting the input string to include such "empty" double quotes is impossible using regular expressions.
Here is a demonstration of only looking ahead:
var message = '"Containing \" escaped quote" "Trailing white-spaces here " " Beginning and trailing white-spaces here " " Beginning white-spaces here"';
console.log(message.split(/\s+(?=")/));
2. Removing nesting quotes
After retrieving the quoted strings, we can remove starting and ending ""
(double quotes) using String.replace()
inside Array.map()
.
Removing the escaping backslash of \"
(escaped double quote) is not needed as it is parsed as "
(double quote). However, our splitting regular expression excludes specifically the escaped double quotes as explained before.
var message = '"Containing \" escaped quote" "Trailing white-spaces here " " Beginning and trailing white-spaces here " " Beginning white-spaces here"';
for (var i of message.split(/(?<=[^\\]")\s+(?=")/).map(i => i.replace(/^"|"$/g, '')))
console.log(i);
Note that String.replace()
using a regular expression with the g
(global) flag is the same as String.replaceAll()
. However, we require a regular expression to only look for the beginning and trailing quotes.