To Get Special Characters from String in jQuery we use the string indexOf function.
1 2 3 4 5 6 7 8 9 10 11 |
var specialChars = "<>@!#$%^&*()_+[]{}?:;|'\"\\,./~`-="; var spArr = []; var check = function(string){ for(i = 0; i < specialChars.length;i++){ if(string.indexOf(specialChars[i]) > -1){ spArr.push(specialChars[i]); } } } check("Bikash#@$%^&*"); document.getElementById("demo").innerHTML = spArr; |
Also Read: 7 Most Useful JavaScript Array Functions
Here are the major steps which we used to get,
- Store possible special characters into a variable.
- Define an empty array to store characters to show.
- Use a loop to check and get all the special characters.
- We use the JS indexOf function to check characters.
- Now we push matched special characters to the defined array.
- At last, we call the function and print matched characters.
Live Example of Getting Special Characters from String in jQuery
See the Pen Get Special Characters from String in jQuery by Bikash Panda (@phpcodertech) on CodePen.
Also Read: Check If String Contains Exact Match in JavaScript
Here is the complete source code to get all special characters from the string. Try it and let me know if you are facing any issues.
Happy Coding..!