how to check for space and special characters in js code example
Example 1: remove special characters from string javascript
var str = "Hello^# World/";
str.replace(/[^a-zA-Z ]/g, "");
Example 2: how to check for special characters in javascript
var format = /[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+/;
if(format.test(string)){
return true;
} else {
return false;
}
Example 3: regex to check if string contains special characters javascript
var format = /[ `!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/;
document.write(format.test("My@string-with(some%text)") + "<br/>");
document.write(format.test("My string with spaces") + "<br/>");
document.write(format.test("MyStringContainingNoSpecialChars"));