Hi Friends đź‘‹,

Welcome To Infinitbility! ❤️

Today, I’m going to show How do I check special characters in strings in Javascript, here I will use regex expression to check special characters in a string or not.

What is a special character?

Special characters are symbols that do not appear on a keyboard. What are considered special characters and symbols will vary depending on your country, your installed language in Word and your keyboard like [], {}, (), #, @ etc.

Let’s start the today’s tutorial How do you check special characters in strings in JavaScript?

Let’s understand how we can do it

  1. Create constant of regex expression
  2. Use the regex with javascript test function
  3. Console which string contains special characters and which not
// Create constant of regex expression
const format = /[ `!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/;

// Use regex with javascript test function
// Console which string contains special characters and which not 
format.test("Hello ( Greetings )"); // true
format.test("Infinitbility"); // false

The above program is the simplest way to check string contains special characters or not, To validate our regex I have passed both case string invalid and valid string, let’s check the output.

javascript, check special characters in string example
javascript, check special characters in string example

I hope it’s help you, All the best 👍.