I had following string ,
const str= "this is harp//foo.eps and harp//data.eps"
I need to replace the string between harp
to .eps
My code below,
const str = "this is harp//foo.eps and harp//data.eps"
const data = str.replace(/\harp.*\eps/, 'www.imge.jpg');
console.log(data);
it was returning output below,
this is www.imge.jpg
But expected output
this is www.imge.jpg and www.imge.jpg
CodePudding user response:
Try to make use non-greedy (?
) search and global (/g
) modifier:
const str = "this is a harp//foo.eps and a harp//data.eps and harp//data.eps"
const data = str.replace(/harp(.*?)eps/g, 'www.imge.jpg');
console.log(data);
CodePudding user response:
You can do it using the replace all function and the following regex
const str = "this is harp//foo-two.eps and harp//data-9.eps"
const newSTr = str.replaceAll(/harp\/\/[A-Za-z\-0-9] \.eps/g, 'www.imge.jpg')
console.log(newSTr);