

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- 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 trim commas with MySQL?
- How to print a number with commas as thousands of separators in JavaScript?
- Replace backward slashes with forward slashes - JavaScript
- Print number with commas as 1000 separators in C#
- Print number with commas as 1000 separators in Python
- How to Replace null with “-” 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
- MongoDB query to convert a string with commas to double
- How can I replace newlines with spaces in JavaScript?
Advertisements