
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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
- Related Articles
- Inverting slashes in a string in JavaScript
- Replace alphabets with nth forward alphabet in JavaScript
- What is Compatibility Testing? Forward & Backward Testing
- How to replace before first forward slash - JavaScript?
- Is there a PHP function that only adds slashes to double quotes NOT single quotes
- Difference between Forward and Backward Reasoning in AI
- How to replace backward "" slash from a string
- Backward compatibility with HTML5
- Replace commas with JavaScript Regex?
- How to Replace null with “-” JavaScript
- Loop backward in array of objects JavaScript
- How to trust backward compatibility with HTML5
- Replace HTML div into text element with JavaScript?
- Replace a letter with its alphabet position JavaScript
- How to replace leading zero with spaces - JavaScript

Advertisements