Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Inverting slashes in a string in JavaScript
We are required to write a JavaScript function that takes in a string that may contain some backward slashes.
And the function should return a new string where all the backslashes with forward slashes.
Therefore, let’s write the code for this function −
Example
The code for this will be −
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
The output in the console will be −
Th\s str\ng \conta\ns some\ forward slas\hes
Advertisements
