# Replacing a string from last

**URL:** <https://es.discourse.group/t/replacing-a-string-from-last/2266>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [January 6, 2025, 4:38am UTC](https://es.discourse.group/t/replacing-a-string-from-last/2266 "2025-01-06T04:38:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tajnur007](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/tajnur007/32/2339_2.png) [@tajnur007](https://es.discourse.group/u/tajnur007)\
**Post date:** [January 6, 2025, 4:38am UTC](https://es.discourse.group/t/replacing-a-string-from-last/2266/1 "2025-01-06T04:38:50Z")

</div>

Hi,

I was implementing something in my project that required replacing a substring from the last (i.e., the last substring from a string if there is more than one matching substring). I checked the string prototypes to do it easily but couldn't find it. The `.replace()` method replaces a substring from the beginning. So I decided to propose the `.replaceLast()` prototype.

I have created a repo with the proposal and looking for a champion to bring it to the committee. I would like to hear more about it if you have any feedback. Also, your suggestion will be appreciated.

Proposal link: [GitHub - tajnur007/proposal-string-replace-from-last: Proposal for String.prototype.replaceLast](https://github.com/tajnur007/proposal-string-replace-from-last)

---

<div class="post-metadata">

**Author:** ![tajnur007](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/tajnur007/32/2339_2.png) [@tajnur007](https://es.discourse.group/u/tajnur007)\
**Post date:** [January 6, 2025, 4:41am UTC](https://es.discourse.group/t/replacing-a-string-from-last/2266/2 "2025-01-06T04:41:06Z")

</div>

Mentioning @aclaymore as you would like to know my proposal.

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [January 12, 2025, 9:12am UTC](https://es.discourse.group/t/replacing-a-string-from-last/2266/3 "2025-01-12T09:12:18Z")

</div>

Hi @tajnur007,

Is there a way to show how prevalent this problem is? Maybe looking at uses of existing libraries: [replace-last - npm](https://www.npmjs.com/package/replace-last)

An alternative API could be adding a 3rd, `position` parameter to `replace` similar to [String.prototype.includes() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes). The index would set the starting point that the search would begin. The caller would still need to do `lastIndexOf` first, but then wouldn't need to create the substring and concat, so would go from 4 operations to 2.

---

<div class="post-metadata">

**Author:** ![tajnur007](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/tajnur007/32/2339_2.png) [@tajnur007](https://es.discourse.group/u/tajnur007)\
**Post date:** [January 15, 2025, 6:24pm UTC](https://es.discourse.group/t/replacing-a-string-from-last/2266/4 "2025-01-15T18:24:59Z")

</div>

The [replace-last - npm](https://www.npmjs.com/package/replace-last) package indicates **approximately ~45K downloads in a week**. It means that operation is crucial in the JS community.

Your alternative solution ( **adding a 3rd, `position` parameter** ) is good but I hope it will be better if we add a method `replaceLast()` which is similar to the `replace()` method. Because we already have a similar naming convention, [Array.prototype.find() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) and [Array.prototype.findLast() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findLast), which do the same operation but in a reverse way.

I understand that adding the `position` parameter to the `replace()` method will provide more flexibility in the replacement operation, I appreciate that. We can also keep both, adding the `replaceLast()` method and adding a 3rd parameter (`position`) to the `replace()` method. Expecting other person opinions to make better decisions.

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [January 15, 2025, 9:02pm UTC](https://es.discourse.group/t/replacing-a-string-from-last/2266/5 "2025-01-15T21:02:42Z")

</div>

45K/week is not a persuasive number, I'm afraid - for example, [p-try - npm](https://www.npmjs.com/package/p-try) which helped to get `Promise.try` moving has 45 _million_ downloads a week.

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [January 15, 2025, 10:10pm UTC](https://es.discourse.group/t/replacing-a-string-from-last/2266/6 "2025-01-15T22:10:21Z")

</div>

Also worth noting that almost all of the download come from [envsub - npm](https://www.npmjs.com/package/envsub) which depends on `replace-last`, and that package is only calling it in one place: [envsub/js/DotEnvParser.js at ba23d838431feb41a7f29f47d0ed2525bf5c9276 · danday74/envsub · GitHub](https://github.com/danday74/envsub/blob/ba23d838431feb41a7f29f47d0ed2525bf5c9276/js/DotEnvParser.js#L28) and I think that one place could be replaced with `string.slice(0, -1)`, so it doesn't actually need the package.

---

<div class="post-metadata">

**Author:** ![lightmare](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/lightmare/32/843_2.png) [@lightmare](https://es.discourse.group/u/lightmare)\
**Post date:** [January 16, 2025, 8:20am UTC](https://es.discourse.group/t/replacing-a-string-from-last/2266/7 "2025-01-16T08:20:28Z")

</div>

> [@aclaymore](#):
>
> An alternative API could be adding a 3rd, `position` parameter to `replace`

A while ago there was a suggestion here to [add splice to String](https://es.discourse.group/t/add-splice-to-string-prototype/2242), which didn't get much positive feedback, but would also allow replace at position -- and IMO that could help other use cases, especially for those scared of regexes -- for example replacing a chunk of text between two found substrings.
