
- 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 use JavaScript to replace a portion of string with another value?
In this article, we are going to explore replacing a portion of a string with another value using JavaScript. We can replace string parts in multiple ways. Below are some common methods −
replace() method
split() method
join() method
Let’s discuss the above methods in detail.
The replace() Method
This is an inbuilt method provided by JavaScript that allows the user to replace a part of a string with some another string or a regular expression. However, the original string will remain the same as earlier.
Syntax
string.replace(searchvalue, newvalue)
Parameters
searchvalue - It is used for searching a string in the whole string.
newvalue - It will replace the searched string.
The function will return a new string as an output to this method.
Example 1
In the below example, we will replace “Hi” with “Welcome To” using the JavaScript replace() method.
# index.html
<html> <head> <title>Replacing String</title> </head> <body> <h1 style="color: green;"> Welcome To Tutorials Point </h1> <script> let string = "Hi Tutorials Point"; /* It will search for Hi and then replace it with the another string */ let replaced_string = string.replace("Hi", "Welcome To"); document.write('<h4>The original string is:</h4>' +string); document.write('<h4>The replaced string is:</h4>' +replaced_string); </script> </body> </html>
Output
The split() Method
We can also use the split() method to split the string into an array of substrings. Once the string is converted into an array, we can compare each string with the string to be searched. When the string is found, we will replace it with the new string. The string split() method takes a separator with which we will split the string.
string.split(separator,limit)
The join() Method
The join() method is used for joining the array elements and returns it as a string. This method has only one optional parameter.
array.join(separator)
Example 2
# index.html
<html> <head> <title>Replacing String</title> </head> <body> <h1 style="color: green;"> Welcome To Tutorials Point </h1> <script> let string = "Start Learning the latest technologies courses now."; let replaced_string = string.split("technologies").join("java"); document.write("<h4>The replaced string is: </h4> " + replaced_string); document.write("<h4>The original string is: </h4>" + string); </script> </body> </html>
Output
- Related Articles
- How to replace all occurrences of a string with another string in Python?
- How to use JavaScript to replace the content of a document with JavaScript?
- Replace part of a string with another string in C++
- Replace String with another in java.
- How to replace all occurrences of a word in a string with another word in java?
- How to get a decimal portion of a number with JavaScript?
- How to replace elements in array with elements of another array in JavaScript?
- How to use Java string replace method?
- Replace one string with another string with Java Regular Expressions
- How to replace all occurrences of a string in JavaScript?
- How to replace total string if partial string matches with another string in R data frame column?
- How to replace string using JavaScript RegExp?
- How to replace the values in a matrix with another value based on a condition in R?
- How to Replace null with “-” JavaScript
- Replace all words with another string with Java Regular Expressions
