Add regex replace and function call docs
This commit is contained in:
parent
54f6713ffe
commit
c0d9b53004
2 changed files with 12 additions and 0 deletions
|
@ -34,4 +34,7 @@ The expression language supports:
|
|||
- strings can contain other expressions again: `'foo {{some_variable}} bar'`
|
||||
- json access (`object.field`, `array[12]`, `object["field"]`)
|
||||
- for this, the object/array value needs to refer to a variable that contains a valid json string.
|
||||
- some function calls:
|
||||
- `round(number, decimal_digits)`: Round a number to the given amount of decimals
|
||||
- `replace(string, regex, replacement)`: Replace matches of a given regex in a string
|
||||
|
||||
|
|
|
@ -230,6 +230,15 @@ fn call_expr_function(name: &str, args: Vec<PrimVal>) -> Result<PrimVal> {
|
|||
}
|
||||
_ => Err(anyhow!("Incorrect number of arguments given to {}", name)),
|
||||
},
|
||||
"replace" => match args.as_slice() {
|
||||
[string, pattern, replacement] => {
|
||||
let string = string.as_string()?;
|
||||
let pattern = regex::Regex::new(&pattern.as_string()?)?;
|
||||
let replacement = replacement.as_string()?;
|
||||
Ok(PrimVal::from(pattern.replace_all(&string, replacement.replace("$", "$$").replace("\\", "$")).into_owned()))
|
||||
}
|
||||
_ => Err(anyhow!("Incorrect number of arguments given to {}", name)),
|
||||
},
|
||||
_ => Err(anyhow!("Unknown function {}", name)),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue