AI-generated Key Takeaways
-
The
String.replacemethod returns a new string with some or all matches of a pattern replaced. -
It takes a regular expression (
regex), areplacementstring, and optionalflags('g' for global, 'i' for ignore case) as arguments. -
The method can be used to replace a specific occurrence or all occurrences of a pattern within a string.
| Usage | Returns |
|---|---|
String.replace(regex, replacement, flags) | String |
| Argument | Type | Details |
|---|---|---|
this: input | String | The string in which to search. |
regex | String | The regular expression to match. |
replacement | String | The string that replaces the matched substring. |
flags | String, default: "" | A string specifying a combination of regular expression flags, specifically one or more of: 'g' (global match) or 'i' (ignore case) |
Examples
Code Editor (JavaScript)
print(ee.String('abc-abc').replace('abc', 'X')); // X-abc print(ee.String('abc-abc').replace('abc', 'X', 'g')); // X-X print(ee.String('abc-abc').replace('abc', '', 'g')); // - print(ee.String('aBc-Abc').replace('abc', 'Z', 'i')); // Z-Abc print(ee.String('aBc-Abc').replace('abc', 'Z', 'ig')); // Z-Z
import ee import geemap.core as geemap
Colab (Python)
print(ee.String('abc-abc').replace('abc', 'X').getInfo()) # X-abc print(ee.String('abc-abc').replace('abc', 'X', 'g').getInfo()) # X-X print(ee.String('abc-abc').replace('abc', '', 'g').getInfo()) # - print(ee.String('aBc-Abc').replace('abc', 'Z', 'i').getInfo()) # Z-Abc print(ee.String('aBc-Abc').replace('abc', 'Z', 'ig').getInfo()) # Z-Z