- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Removing punctuations from a string using JavaScript
Problem
We are required to write a JavaScript function that takes in a string and removes all special characters from the string leaving behind just alphabets and numerals in the resultant string.
Input
const str = 'th@is Str!ing Contains 3% punctuations';
Output
const output = 'thisStringContains3punctuations';
Because we removed all punctuations and whitespaces
Example
Following is the code −
const str = 'th@is Str!ing Contains 3% punctuations'; const removeSpecialChars = (str = '') => { let res = ''; for(let i = 0; i < str.length; i++){ const el = str[i]; if(+el){ res += el; }else if(el.toLowerCase() !== el.toUpperCase()){ res += el; }; continue; }; return res; }; console.log(removeSpecialChars(str));
Output
thisStringContains3punctuations
- Related Articles
- Removing all spaces from a string using JavaScript
- Count total punctuations in a string - JavaScript
- Removing adjacent duplicates from a string in JavaScript
- How to Remove Punctuations From a String in Python?
- Removing a specific substring from a string in JavaScript
- Removing first k characters from string in JavaScript
- Removing comments from array of string in JavaScript
- Removing all non-alphabetic characters from a string in JavaScript
- Removing Elements from a Double Linked List using Javascript
- Removing n characters from a string in alphabetical order in JavaScript
- Constructing a sentence based on array of words and punctuations using JavaScript
- Removing leading zeros from a String using apache commons library in Java
- Breaking a string into chunks of defined length and removing spaces using JavaScript
- Removing Brackets from an Algebraic String Containing + and – Operators using C++
- Removing consecutive duplicates from strings in an array using JavaScript

Advertisements