1

I am looking for a JavaScript function that checks for the following characters (without commas) in a string. If they are present, it would return false, else returns true.

<,>,(,),#,"",',:,::

1 Answer 1

4
function (str) { 
   return ! (/[<>()#':]|""/.test(str));
}

any set of single characters can put put inside a [set of brackets]. For longer patterns, use the pipe.

edit: as patrick pointed out, if you're checking for : you don't need to check for :: separately.

Sign up to request clarification or add additional context in comments.

3 Comments

@patrick Funny, I gave @nickf +1 because his didn't negate!
@Josh - I didn't catch this at first, but the request was specifically to return false if the character was found.
Although, it is a bit redundant to check for both : and :: since you can't have the second without the first.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.