
- 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
JavaScript Regex to remove text after a comma and the following word?
Let’s say the following is our string −
var sentence = 'My Name is John, Smith I live in US'; console.log("The original value="+sentence);
We need to remove the text after comma and the following word i.e. after removing “I live in US” and keeping the rest. This would be the resultant string −
My Name is John, Smith
For this, use match() along with split().
Example
var sentence = 'My Name is John, Smith I live in US'; console.log("The original value="+sentence); var expression = sentence.match(/([^,]*)(.*)/)[1]; var positionForComma = sentence.match(/([^,]*),(.*)/)[2].split(' ')[1] var newValue = expression + ', ' + positionForComma console.log("Updated="+newValue);
To run the above program, you need to use the following command −
node fileName.js.
Here, my file name is demo175.js.
Output
This will produce the following output −
PS C:\Users\Amit\javascript-code> node demo175.js The original value=My Name is John, Smith I live in US Updated=My Name is John, Smith
- Related Articles
- Remove specific word in a comma separated string with MySQL
- How to Add a Comma after the First Word in Each Cell in Excel?
- Split the sentences by comma and remove surrounding spaces - JavaScript?
- Match specific word in regex in JavaScript?
- MySQL Query to remove all characters after last comma in string?
- JavaScript Regex to remove leading zeroes from numbers?
- Capitalize only the first letter after colon with regex and JavaScript?
- Regex to find string and next character in a comma separated list - MySQL?
- How to remove non-word characters in JavaScript?
- Linux Commands – Remove All Text After X
- Possible to split a string with separator after every word in JavaScript
- Java regex program to add space between a number and word in Java.
- How to match a non-word character using Java RegEx?
- How to match word characters using Java RegEx?
- How to match word boundaries using Java RegEx?

Advertisements