[javascript] How do you judge a character fragment and whether it is a purely legal javascript string format?

  node.js, question

"abc"Legal stringtrue

'abc'Legal stringtrue

A plus "abc"Variable plus stringfalse

"a" plus "b plus" c "Variable plus stringfalse

""abc""Illegal formatfalse

'"abc""Illegal formatfalse

'"abc"'Legal stringtrue

"'abc'"Legal stringtrue

"abc" plus "cde"Legal stringtrue

Wait …

At present the landlord’s method

var regexString = /^["'](.*)["']$/gi

Obviously, there are many situations where the correct results cannot be obtained.

Ask for advice. Do you have a good solution or do you know that npm wheels are available?

There is a reluctant solution is to use

function testStr(str){
 return (typeof eval(str)) === "string"
 //false if there is an error
 return false;
 return true;
TestStr("true") // boolean value false
 testStr("\"true\"") //true
 testStr("\'true\"") //false

I wonder if there is a better one.