1

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

1
  • 1
    I think you want the non-greedy version .*? Commented Sep 12, 2022 at 10:33

2 Answers 2

1

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);

Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.