Here’s an example of how it can replace strings when it matches the substring:
The result would look like this:
You’d notice that only that the first ‘Hi’ in the sentence got replaced with ‘Hello’ but the second ‘Hi’ remains as such. This is because when we pass a substring to the replace() method, it only replaces the first occurrence of that substring.
You can, however, use a regular expression to replace all the substrings instead:
Now the output would change to:
If you have any questions or suggestions, let me know in the comments below!