- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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
Replace backward slashes with forward slashes - JavaScript
We are required to write a JavaScript function that takes in a string that may contain some backward slashes. The function should return a new string with all the backward slashes replaced with forward slashes.
Let’s say the following is our string −
const str = 'Th/s str/ng /conta/ns some/ forward slas/hes';
Example
Following is the code −
const str = 'Th/s str/ng /conta/ns some/ forward slas/hes'; const invertSlashes = str => { let res = ''; for(let i = 0; i < str.length; i++){ if(str[i] !== '/'){ res += str[i]; continue; }; res += '\'; }; return res; }; console.log(invertSlashes(str));
Output
This will produce the following output in console −
Th\s str
g \conta
s some\ forward slas\hes
Advertisements