- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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().
var sentence = 'My Name is John, Smith I live in US'; console.log("The original value="+sentence); var expression = sentence.match(/([^,]*)(.*)/); var positionForComma = sentence.match(/([^,]*),(.*)/).split(' ') var newValue = expression + ', ' + positionForComma console.log("Updated="+newValue);
To run the above program, you need to use the following command −
Here, my file name is demo175.js.
This will produce the following output −
- Remove specific word in a comma separated string with MySQL
- MySQL Query to remove all characters after last comma in string?
- Regex to find string and next character in a comma separated list - MySQL?
- Java regex program to add space between a number and word in Java.
- Remove comma from a string in PHP?
- How to match a non-word character using Java RegEx?
- How to find and replace the word in a text file using PowerShell?
- How to match word characters using Java RegEx?