@@ -686,10 +686,10 @@ defmodule String do
686
686
Returns a new binary created by replacing occurences of `pattern` in
687
687
`subject` with `replacement`.
688
688
689
- By default, it replaces all occurences, except if the `global` option is
689
+ By default, it replaces all occurences, unless the `global` option is
690
690
set to `false`.
691
691
692
- A `pattern` may be a string or a regular expression.
692
+ The `pattern` may be a string or a regular expression.
693
693
694
694
## Examples
695
695
@@ -699,18 +699,20 @@ defmodule String do
699
699
iex> String.replace("a,b,c", ",", "-", global: false)
700
700
"a-b,c"
701
701
702
- The pattern can also be a regular expression. In those cases , one can give `\N` or
702
+ When the pattern is a regular expression, one can give `\N` or
703
703
`\g{N}` in the `replacement` string to access a specific capture in the
704
- regex :
704
+ regular expression :
705
705
706
706
iex> String.replace("a,b,c", ~r/,(.)/, ",\\1\\g{1}")
707
707
"a,bb,cc"
708
708
709
709
Notice we had to escape the escape character `\`. By giving `\0`,
710
710
one can inject the whole matched pattern in the replacement string.
711
711
712
- When strings are used as a pattern, a developer can also use the
713
- replaced part inside the `replacement` via the `:insert_replaced` option:
712
+ When the pattern is a string, a developer can use the replaced part inside
713
+ the `replacement` by using the `:insert_replace` option and specifying the
714
+ position(s) inside the `replacement` where the string pattern will be
715
+ inserted:
714
716
715
717
iex> String.replace("a,b,c", "b", "[]", insert_replaced: 1)
716
718
"a,[b],c"
@@ -721,6 +723,8 @@ defmodule String do
721
723
iex> String.replace("a,b,c", ",", "[]", insert_replaced: [1, 1])
722
724
"a[,,]b[,,]c"
723
725
726
+ If any position given in the `:insert_replace` option is larger than the
727
+ replacement string, or is negative, an `ArgumentError` is raised.
724
728
"""
725
729
@ spec replace ( t , pattern | Regex . t , t , Keyword . t ) :: t
726
730
def replace ( subject , pattern , replacement , options \\ [ ] ) when is_binary ( replacement ) do
0 commit comments