Validate only alpha characters, single/double quotes, space and comma in array in Javascript

You cannot invert a RegExp by using a ! in the constructor. Simply try this:

array = ['hello', 'hello'];
array1 = ['hello', 'hello1'];

var arrayChar = new RegExp(/^[a-z',"\s]+$/i); // This means the string must contain only what is between brackets, at least 1 character.

function validateArrayChar(array,char) {
  return array.every(function(element) { 
    return arrayChar.test(element);

console.log(validateArrayChar(array1, arrayChar)); // Will return false, at least one character is invalid
console.log(validateArrayChar(array, arrayChar)); // Will return true

For Regex reference:

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top