javascript - 如何用找到的不区分大小写的正则表达式替换匹配项,但扩展结果
问题描述
简单地说,我有字符串:Hello and good morning world.
并且我有一个搜索字符串:Good
。
我想匹配搜索字符串不区分大小写的所有内容,但我需要将结果扩展为:
Hello and <mark>good</mark> morning world.
我还在做一些研究,但它看起来比我想象的要复杂。我希望你能帮忙。到目前为止,我找不到任何类型的线程。希望你能给我指出一个甚至解决这个问题。非常感谢。
解决方案
replace
通过在正则表达式中指定忽略和全局大小写,您可以使用字符串函数获得预期结果:
var string = 'Hello and good morning world.';
var result = string.replace(/(Good)/ig, '<mark>$1</mark>');
console.log(result);
推荐阅读
- java - 固定大小的长数组的运行时间
- java - 如何获取具有特定格式的子字符串
- java - 内置短方法来获取 Hashmap 的键数组,按它们在 Java 中的值排序
- aws-glue - Aws Glue:如何持久化 DynamicFrame
- python - django 从 mysql 数据库中检索数据
- r - 为什么“$”自动补全适用于 BioConductor 的 S4 类“SummarizedExperiment”
- ios - iOS是否存储exif数据,iOS中是否有从照片中读取位置数据的新方法?
- java - Java - 检查重复的 ArrayList
- python - 无法对 google automl 进行预测
- javascript - 使用多个动态用户输入进行过滤。如何更有效地进行过滤?