
- 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 commas with JavaScript Regex?
Let’s say the following are our strings with commas −
"My Favorite subject is," "My Favorite subject is, and teacher name is Adam Smith" "My Favorite subject is, and got the marks 89"
To replace commas, use replace and in that, use Regular Expression. Following is the code −
Example
const makingRegularExpression = /,(?=[^,]*$)/; replaceComma("My Favorite subject is,"); replaceComma("My Favorite subject is, and teacher name is Adam Smith"); replaceComma("My Favorite subject is, and got the marks 89"); function replaceComma(values){ console.log(values, " ==== replaced by JavaScript ==== ", values.replace(ma kingRegularExpression, " JavaScript")); }
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo164.js.
Output
This will produce the following output −
PS C:\Users\Amit\javascript-code> node demo164.js My Favorite subject is, ==== replaced by JavaScript ==== My Favorite sub ject is JavaScript My Favorite subject is, and teacher name is Adam Smith ==== replaced by JavaScript ==== My Favorite subject is JavaScript and teacher name is Ad am Smith My Favorite subject is, and got the marks 89 ==== replaced by JavaScript ==== My Favorite subject is JavaScript and got the marks 89
- Related Articles
- JavaScript regex - How to replace special characters?
- Dynamically replace data based on matched RegEx - JavaScript?
- Replace parts of a string with C# Regex
- How to Replace null with “-” JavaScript
- How to trim commas with MySQL?
- How to print a number with commas as thousands of separators in JavaScript?
- Replace backward slashes with forward slashes - JavaScript
- Replace HTML div into text element with JavaScript?
- Replace a letter with its alphabet position JavaScript
- How to replace leading zero with spaces - JavaScript
- Replace alphabets with nth forward alphabet in JavaScript
- How to replace line breaks with using JavaScript?
- Validate input: replace all ‘a’ with ‘@’ and ‘i’ with ‘!’JavaScript
- String replace multiple characters with an asterisk in JavaScript
- Capitalize a word and replace spaces with underscore - JavaScript?

Advertisements