
- 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
How to replace before first forward slash - JavaScript?
Let’s say the following is our string with forward slash −
var queryStringValue = "welcome/name/john/age/32"
To replace before first forward slash, use replace() along with regular expressions.
Example
Following is the code −
var regularExpression = /^[^/]+/ var queryStringValue = "welcome/name/john/age/32" var replacedValue = queryStringValue.replace(regularExpression, 'index'); console.log("Original value="+queryStringValue); console.log("After replacing the value="+replacedValue);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo245.js.
Output
This will produce the following output on console −
PS C:\Users\Amit\javascript-code> node demo245.js Original value=welcome/name/john/age/32 After replacing the value=index/name/john/age/32
- Related Articles
- Split a URL in JavaScript after every forward slash?
- How to replace backward "" slash from a string
- Replace backward slashes with forward slashes - JavaScript
- Replace alphabets with nth forward alphabet in JavaScript
- How to extract string before slash from a vector in R?
- Update all varchar column rows to display values before slash in MySQL?
- MySQL query to replace part of string before dot
- How to Replace null with “-” JavaScript
- How to replace string using JavaScript RegExp?
- JavaScript regex - How to replace special characters?
- How to replace leading zero with spaces - JavaScript
- How to replace line breaks with using JavaScript?
- Python program to replace first ‘K’ elements by ‘N’
- How to use JavaScript to replace the content of a document with JavaScript?
- How to replace only the first repeated value in a string in MySQL

Advertisements