当前位置: 首页 > news >正文

js 字符串中的特殊字符全部替换成定义对象里面key对应的value值(进阶篇)

在上一篇文章中,初步介绍了字符串特殊字符的基础用法,js 字符串中的特殊字符全部替换成定义对象里面key对应的value值(基础篇)-CSDN博客可以回顾看一下。现在,进一步封装方法全局调用。

示例1
let textString = "你好!my name is ${name},l like ${hobby},this is a test string with special characters!";let userInfo = {name: 'Aotman_',hobby:"playing basketball"}let overString = textString.replace(/\$\{(.*?)\}/g,function(textString,i){console.log(textString, 'str');console.log(i, 'iii');return userInfo[i];});console.log(overString, '输出结果'); // 输出: "你好!my name is Aotman_,l like playing basketball,this is a test string with special characters!"
示例2
    getReplaceStr(str, obj) {for (let key in obj) {str = str.replace(new RegExp('\\{\\{' + key + '\\}\\}', 'g'), obj[key])}return str}let userInfo = {name: 'Aotman_',hobby:"playing basketball"}let textString = `你好!my name is {{name}},l like {{hobby}},this is a test string with special characters!`console.log(this.getReplaceStr(textString, userInfo)); // 输出: "你好!my name is Aotman_,l like playing basketball,this is a test string with special characters!"

示例3
 getReplaceStr(textString, userInfo) {return textString.replace(/{(.*?)}/g, (match, key) => {return typeof userInfo[key] !== 'undefined' ? userInfo[key] : match;});
}
const textString= "你好!my name is {name},l like {hobby},this is a test string with special characters!Hello, {name}! Welcome to {city}.";
const userInfo= {name: 'Aotman_',city:"Hang Zhou",hobby:"playing basketball"}
const overString= this.getReplaceStr(textString, userInfo);
console.log(overString); // 你好!my name is Aotman_,l like playing basketball,this is a test string with special characters!Hello, Aotman_! Welcome to Hang Zhou.

代码解析

1、函数定义:我们定义了一个名为 getReplaceStr的函数,它接受两个参数:textString和 userInfo。

2、正则表达式:/{(.*?)}/g 用于匹配字符串中的占位符。

3、替换逻辑:textString.replace(...) 方法将每个占位符替换为对象 userInfo 中相应的值。如果找不到对应的值,则保持原样。

进阶扩展:
    getReplaceStr(textString) {const userInfo= {'name': 'Aotman_','city':"Hang Zhou",'hobby':"playing basketball",'.':"。"}return textString.trim().replace(new RegExp(Object.keys(userInfo).join('|'), 'g'), match => {return typeof userInfo[match] !== 'undefined' ? userInfo[match] : match})},
const textString = "你好!my name is name,l like hobby,this is a test string with special characters!Hello, name! Welcome to city.";
const result = this.getReplaceStr(textString);
console.log(result);//你好!my Aotman_ is Aotman_,l like playing basketball,this is a test string with special characters!Hello, Aotman_! Welcome to Hang Zhou。

结论

本文介绍了 JavaScript 的占位符替换方案,提供了灵活易懂的代码示例和实现步骤。无论是刚开始学习前端技术,还是正在从事前端开发工作,掌握字符串占位符替换的技巧都是十分重要的。希望通过本文,你能够更深入理解字符串处理的强大,提升开发技能,为未来的项目奠定坚实基础。如果你有任何问题或建议,欢迎留言讨论!

http://www.xdnf.cn/news/407125.html

相关文章:

  • Python60日基础学习打卡D12【虫豸版】
  • 如何使用 React Hooks 替代类组件的生命周期方法?
  • Linux服务器连接SSH工具FinalShell安装使用支持Linux文件上传下载
  • (自用)Java学习-5.8(总结,springboot)
  • 【合新通信】无人机天线拉远RFOF(射频光纤传输)解决方案
  • upload-labs通关笔记-第01关 文件上传之前端绕过(3种渗透方法)
  • 浙江大学 deepseek 公开课 第三季 第3期 - 陈喜群 教授 (附PPT下载) by 突破信息差
  • Linux笔记---信号(上)
  • SWMM在城市排水防涝规划中的实战应用:模型校准、情景模拟与工程决策
  • Linux进程10-有名管道概述、创建、读写操作、两个管道进程间通信、读写规律(只读、只写、读写区别)、设置阻塞/非阻塞
  • WordPress 网站上的 jpg、png 和 WebP 图片插件
  • 请解释 React Native 的新架构(Fabric 和 TurboModules)与旧架构的主要区别
  • 「光域」系列激光测距传感器:以光为尺,重构空间认知边界
  • 【华为HCIP | 华为数通工程师】821—多选解析—第二十二页
  • 详解 IRC协议 及客户端工具 WeeChat 的使用
  • OpenCV进阶操作:光流估计
  • Linux基础命令之目录管理——了解各种操作文件目录的命令,万字教学,超详细!!!(1)
  • OCCT知识笔记之分解BOX
  • 计算频谱的方法
  • 《基于 Kubernetes 的 WordPress 高可用部署实践:从 MariaDB 到 Nginx 反向代理》
  • 《AI大模型应知应会100篇》第59篇:Flowise:无代码搭建大模型应用
  • 免费批处理软件一键修改文件名称
  • 了解docker-compose.yml
  • mac一键安装gpt-sovit教程中,homebrew卡住不动的问题
  • latex控制表格宽度,不要超出页面
  • windows系统使用phpstudy安装ssl证书
  • 机器学习驱动的智能化电池管理技术与应用
  • 腾讯怎样基于DeepSeek搭建企业应用?怎样私有化部署满血版DS?直播:腾讯云X DeepSeek!
  • 解决 CJSON 浮点数精度问题:从 `cJSON_AddNumberToObject` 到 `cJSON_AddRawToObject`
  • IoTDB 分段查询语句深度剖析:GROUP BY 与时序语义的完美结合