I want to replace the first part of regex for a URL with asterisks. Depending on the regex, for example:
Case 1
http://example.com/pat
You can use the sticky flag y (but Internet Explorer doesn't support it):
s = s.replace(/(^https?:\/\/.*?\/path1\/?|(?!^))./gy, '$1*')
But the simplest (and that is supported everywhere), is to use a function as replacement parameter.
s = s.replace(/^(https?:\/\/.+\/path1\/?)(.*)/, function (_, m1, m2) {
return m1 + '*'.repeat(m2.length);
});
For the second case, you can simply check if there's an @
after the current position:
s = s.replace(/.(?=.*@)/g, '*');