How to check if a string contains an array?

I want to write a function that accepts a string and returns a boolean value based on whether or not that string contains an array (syntax).

And I need help with the regex pattern.

Here’s an example of a string that would return true:

'["Visual Thinking", "GitHub", "Any"]'

Here’s an example of a string that would return false:

"This string does not contain array syntax"

Until now you have been developing the following pattern

/[(("w*")(,)?)*]/g

Answer

You can detect whether string is an array or not using JSON.parse

const str = '["Visual Thinking", "GitHub", "Any"]';
let isArr;

try {
  const arr = JSON.parse(str);
  if (Array.isArray(arr)) isArr = true;
} catch (error) {
  isArr = false;
}

console.log(isArr);