Warning: session_start(): open(/tmp/sess_fe9e7b0cf62653d312b599dc0b24cdb2, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Check if String contains Special Characters in JavaScript? - LearnShareIT

How To Check if String contains Special Characters in JavaScript?

Check if String contains Special Characters in JavaScript

The test() function is one of the methods that can be used to check if a string contains special characters in JavaScript. In addition you can find some other methods introduced in this article. Please read the article below and draw your ways to do it.

What are special characters?

Special characters are characters that are not written in the standard form. From the outside, it has strange shapes. They do not need to adhere to a specific rule when paired together. Each combination carries a unique meaning and is according to the purpose of its creator.

Therefore, it is difficult for an outsider to explain what the particular character combination means clearly. Usually, when looking at those lines of characters, one can only see them as beautiful.

Regex

The term “regular expression,” also known as its expression, is utilized for advanced string processing. These expressions will have their own rules, which you must adhere to for your expression to be novel. The String can also shorten to RegEx and the Regular name Expression.

Some methods in javascript

To determine whether a pattern is present in a string, use the test() or search() methods; the exec() or match() methods provide additional information at a slower execution rate.

Code:

var regexIT = /LearnShareIT/;
var string = "Welcome to LearnShareIT";
console.log(regexIT.test(string));

Output:

true

So we can see that the regexIT we tested is present in the given string, so it returns true in the console screen.

Check if string contains special characters in JavaScript

Using test() method

In this method, we will use the test() method to check if the String contains special characters or not. We first write a regex of special characters and then check the String. See the example below to see how to solve it.

Syntax:

RegExpObject.test(string)

Parameter:

  • string: required.

Code:

var regexIT = /[`!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/;
var string = "Welcome to LearnShareIT!";
console.log(regexIT.test(string));

Output:

true

In the above example, we see a unique character in the string, and the character is “!” so the program will return true to the console screen.

Using match() method

The Match() method will search for substrings that match the provided regular expression. The method will return the found strings as an array.

Syntax:

match(regexp)

Parameter:

  • Regexp: an object with a regular expression or any object with the Symbol. 

Code:

var regexIT = /[`!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/;
var string = "Welcome to LearnShareIT!";
console.log(string.match(regexIT));

Output:

['!', index: 23, input: 'Welcome to LearnShareIT!', groups: undefined]

When you use the match() method in the above example, it will find the substring that matches the regex we have given and here the string “!”. The result returned on the console screen will be that substring value along with the starting position of the String and, finally, the input value.

Summary

To summarize, this article shows you two ways to check if a string contains special characters in JavaScript using the test() method or match() method to get the same result. If you have any problems, please comment below. We will respond as possible. Thanks for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *