- 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
Replacing all special characters with their ASCII value in a string - JavaScript
We are required to write a JavaScript function that takes in a string that might contain some special characters. The function should return a new string should have all special characters replaced with their corresponding ASCII value
Example
Following is the code −
const str = 'Th!s !s @ str!ng th@t cont@!ns some special characters!!'; const specialToASCII = str => { let res = ''; for(let i = 0; i < str.length; i++){ if(+str[i] || str[i].toLowerCase() !== str[i].toUpperCase() || str[i] === ' '){ res += str[i]; continue; }; res += str[i].charCodeAt(0); }; return res; }; console.log(specialToASCII(str));
Output
This will produce the following output in console −
Th33s 33s 64 str33ng th64t cont6433ns some special characters3333
- Related Articles
- Replacing vowels with their 1-based index in a string in JavaScript
- Replacing every nth instance of characters in a string - JavaScript
- Write a Regular Expression to remove all special characters from a JavaScript String?
- Finding count of special characters in a string in JavaScript
- Search a string with special characters in a MongoDB document?
- Replacing dots with dashes in a string using JavaScript
- MySQL query to select a specific string with special characters
- Convert all lowercase characters to uppercase whose ASCII value is co-prime with k in C++
- How to remove all special characters, punctuation and spaces from a string in Python?
- How to replace all the special characters following another character – JavaScript?
- Removing all non-alphabetic characters from a string in JavaScript
- Replacing upperCase and LowerCase in a string - JavaScript
- Fetch a specific record from a column with string values (string, numbers and special characters) in MySQL
- Check if string contains special characters in Swift
- Convert Hexadecimal value String to ASCII value String in C++

Advertisements