How to Check Whether a String Does Not Start With a Regex in JavaScript

0
148
How to Filter an Object by Key in JavaScript


Dec 29, 2021

To check if a string does not start with specific characters using a regular expression, use the test() function and negate it.
Make sure your regular expression starts with ^, which is a special character that represents the start of the string.

function doesNotStartWithA(str) {
  return !/^A/.test(str);
}

Another approach is to use [^A].
[] denotes a set of characters to match, and ^ at the start of the set negates the set.
So [^A] matches any character other than A.

function doesNotStartWithA(str) {
  return /^[^A]/.test(str);
}

More Fundamentals Tutorials



Source link

Leave a reply

Please enter your comment!
Please enter your name here