参数对象,支持字符串、数字、布尔值、null、undefined
Optional
options: ReplaceVariablesOptions = {}配置选项
变量包裹符号
是否严格模式,如果为true,未找到的变量会抛出错误
未找到变量时的默认值,仅在非严格模式下生效
import { replaceVariablesInString } from '@compass-aiden/utils';
// 基础用法
replaceVariablesInString('Hello {{name}}!', { name: 'World' }); // 'Hello World!'
// 支持多种数据类型
replaceVariablesInString('用户ID: {{id}}, 年龄: {{age}}, 是否VIP: {{isVip}}', {
id: 12345,
age: 25,
isVip: true
}); // '用户ID: 12345, 年龄: 25, 是否VIP: true'
// 自定义包裹符号
replaceVariablesInString('Hello [name]!', { name: 'World' }, {
wrapper: ['[', ']']
}); // 'Hello World!'
// 严格模式
replaceVariablesInString('Hello {{name}}!', {}, { strict: true }); // 抛出错误
// 默认值
replaceVariablesInString('Hello {{name}}!', {}, {
defaultValue: 'Guest'
}); // 'Hello Guest!'
字符串模板