Q:

Replace and Keep Some part of the string in Node js

How can I do following in Node js/Javascript?
Eg. Say original occurence = "xyz-abc-def"
I wanted to replace whole occurrences of above string by "123-abc-456". xyz and def are constant in above string while abc can be any string in original occurence. Eg. 
"xyz-asd-def" -> "123-asd-456"
"xyz-ghj-def" -> "123-ghj-456"

How can I do that in node js?

A:

You can do this with a regular expression.
var reg = /xyz-(.+?)-def/g;
var input = 'xyz-asd-def';
var result = input.replace(reg, '123-$1-456');

