0

I want to disallow a string that has square brackets when using test method in regex.

 function validateName(name){
    var nameRegex = /^[A-zA-z\s][A-zA-z\s.-]{1,64}$/i;
    console.log('##########Checking validation.........');
    return nameRegex.test(name);
}
bobble bubble
  • 16,888
  • 3
  • 27
  • 46
  • 1
    Does this answer your question? [Difference between regex \[A-z\] and \[a-zA-Z\]](https://stackoverflow.com/questions/4923380/difference-between-regex-a-z-and-a-za-z), [\[A-z0-9\]+ regexp matching square brackets](https://stackoverflow.com/questions/28449927/a-z0-9-regexp-matching-square-brackets)... It should be [`^[A-Za-z\s][A-Za-z\s.-]{1,64}$`](https://regex101.com/r/sWiQ2i/1) – bobble bubble Dec 28 '22 at 02:25

1 Answers1

1

Try this regex and let me know:

/^[^\[\]]*$/

EDIT:

const regex = /^[^\[\]]*$/;

console.log(regex.test('ok')) // return true
console.log(regex.test('[ok')) // return false
console.log(regex.test('ok]')) // return false
Paul Serre
  • 457
  • 3
  • 11