I'm splitting code via regular expressions and it's doing (sort of) the right thing... until the string ends. Would this be a problem with my regex? If so, how would I fix it?
Code:
let args = line.split(/\s(?=(([^"]*"){2})*[^"]*$)/);
console.log("LINE: " line)
console.log("SPLIT: " JSON.stringify(args))
Output:
LINE: var String: test = "Hello"
SPLIT: ["var","String: test = \"Hello\"","Hello\"","String:","test = \"Hello\"","Hello\"","test","= \"Hello\"","Hello\"","=","\"Hello\"","Hello\"","\"Hello\""]
Expected Output:
LINE: var String: test = "Hello"
SPLIT: ["var", "String:", "test", "=", "\"Hello\""]
CodePudding user response:
I would use String.match(regex)
not String.split(regex)
. I would design a regex that has the following characteristics:
- Capture
=
, or - Capture quoted string, or
- Capture unquoted alphabet string
- And add an optional ':' suffix
e.g.
let line = `var String: test = "Hello"`;
let m = line.match(/(=|"[^"]*"|\w ):?/g);
console.log(m);
// [ "var", "String:", "test", "=", "\"Hello\"" ]