AngeloC AngeloC - 2 months ago 23
Javascript Question

How to replace sub string after the portion of match in javascript?

I'd like to change everything after '=img' into something else, following code does not work:

var j = '/test/123=img?xyze'
j.replace(/=img\.*/, '');

Any idea how? Thanks,


var j = '/test/123=img?xyze'
j=j.replace(/(=img)(.*)/, '$1_somethingElse');
console.log(j); //  "/test/123=img_somethingElse"

$1 is =img and the rest would be in $2 if you needed it.

Further reading: