Chacha
Chacha

Reputation: 441

Part of string search and replace in Node JS

I wanted to do some regex search on strings and remove that part of the string that matches.

Eg. I have following strings

xyz-v1.0.0#
abc-v2.0.0#
def-v1.1.0#

So, for such cases, I wanted to remove -v1.0.0#, -v2.0.0# and -v1.1.0# from these strings.

So, for this what regex can I use and how can I remove them in Node JS?

Upvotes: 0

Views: 538

Answers (1)

kakamg0
kakamg0

Reputation: 1096

You can do this

.replace(/-.*$/, '') will check for -{anything} at the end of the string and replace with nothing.

const strs = [
  'xyz-v1.0.0#',
  'abc-v2.0.0#',
  'def-v1.1.0#'
];
const newStrs = strs.map(str => str.replace(/-.*$/, ''));
console.log(newStrs);

Upvotes: 1

Related Questions