> For the complete documentation index, see [llms.txt](https://osl.mistium.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://osl.mistium.com/methods/strings/.replacefirst.md).

# .replaceFirst(old,new)

## Description

replaceFirst substitutes only the first occurrence of a substring with a new substring within a string. Unlike .replace() which replaces all occurrences, this method only changes the first match.

## Parameters

replaceFirst needs two parameters:

* old: the substring to search for
* new: the substring to replace it with

## Usage On Strings

```javascript
str = "hello hello world"

log str.replaceFirst("hello", "hi")
// "hi hello world"
// only the first "hello" is replaced

str = "one two one two"
log str.replaceFirst("one", "1")
// "1 two one two"
// subsequent matches remain unchanged

str = "aaa"
log str.replaceFirst("a", "b")
// "baa"
// only first character is replaced

str = "no matches here"
log str.replaceFirst("xyz", "abc")
// "no matches here"
// if the old substring isn't found, the original string is returned unchanged
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://osl.mistium.com/methods/strings/.replacefirst.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
