主题
replace
替换一个或多个字符串或正则表达式。
语法
knap
{{ value | replace:"old":"new" }}
{{ value | replace:("a":"b", "c":"d") }}用法
- 每个带引号的查找取值后面跟一个冒号及其替换内容。
- 正则表达式可以带标志,例如
"/[aeiou]/g"。 - 多个替换按从左到右的顺序应用。
- 使用空替换内容即可删除匹配到的文本。
- 支持的正则表达式标志为
g、i、m、s、u和y。若要按字面匹配冒号或管道等模板标点,请用反斜杠转义。
示例
移除文本
json
{
"message": "hello, world!"
}knap
{{ message | replace:",":"" }}md
hello world!多重替换
json
{
"message": "hello world"
}knap
{{ message | replace:"e":"a","o":"0" }}md
hall0 w0rld正则表达式
json
{
"message": "hello world"
}knap
{{ message | replace:"/[aeiou]/g":"*" }}md
h*ll* w*rld不区分大小写的匹配
json
{
"message": "HELLO world"
}knap
{{ message | replace:"/hello/i":"hi" }}md
hi world