
- 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 multiple instances of text surrounded by specific characters in JavaScript?
Let’s say the following is our string. Some text is surrounded by special character hash(#) −
var values = "My Name is #yourName# and I got #marks# in JavaScript subject";
We need to replace the special character with valid values. For this, use replace() along with shift().
Example
Following is the code −
var values = "My Name is #yourName# and I got #marks# in JavaScript subject"; const originalValue = ["David Miller", 97]; var result = values.replace(/#([^#]+)#/g, _ => originalValue.shift()); console.log(result);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo298.js.
Output
This will produce the following output on console −
PS C:\Users\Amit\javascript-code> node demo298.js My Name is David Miller and I got 97 in JavaScript subject
- Related Articles
- String replace multiple characters with an asterisk in JavaScript
- Excel batch find and replace specific text in hyperlinks
- JavaScript regex - How to replace special characters?
- Replace array value from a specific position in JavaScript
- Name some methods of weakMap instances in javascript?
- Accessing Clipboard Contents Across Multiple Instances of Vim from Terminal
- Replace HTML div into text element with JavaScript?
- Replacing specific characters from a matched string in PHP regular expression where we don't know the number of instances of the match?\n
- How to replace characters except last with a mask character in JavaScript?
- Replace tab characters by a fixed tabsize in a string array in Numpy
- How to remove all instances of a specific character from a column in MySQL?
- How to replace all the special characters following another character – JavaScript?
- Hide div that contains specific text with JavaScript?
- Replace characters in a string in Arduino
- Sorting string characters by frequency in JavaScript

Advertisements