JavaScript 替换为正则表达式
有时,我们想要替换字符串中的特定单词或编写正则表达式(也称为正则表达式)以用新值替换模式。
为此,如果我们有一个小字符串,我们可以获得 replace()
函数的优势,或者可以使用 RegExp
对象的方法在长字符串(段落)中查找模式并将其替换为新单词/值.
在 JavaScript 中使用 replace()
函数
replace()
函数搜索字符串并将其替换为新字符串而不更改原始字符串。此函数替换第一个匹配项,然后停止。
这就是为什么我们使用正则表达式来满足我们验证文本和搜索文本的需求。我们可以使用正则表达式执行全局、多行或不区分大小写的匹配来替换字符串。
示例代码:
let p = 'Hello JavaScript World, I am diving into the javascript world.'
console.log(p.replace('JavaScript', 'Python'));
输出:
"Hello Python World, I am diving into the javascript world."
在上面给出的输出中,我们可以观察到 replace()
函数在替换第一个匹配项后停止。此限制导致带有修饰符的正则表达式,也称为标志。
在 JavaScript 中替换为 Regex(正则表达式)
我们使用 i
标志进行不区分大小写的搜索,这意味着 World
和 world
是相同的。test()
方法检查并在模式存在时返回 true
。
示例代码:
let p = 'Hello JavaScript World, I am diving into the javascript world.'
const pattern = /javascript/i;
if (pattern.test(pattern)) {
let result = p.replace(pattern, 'Python');
console.log(result);
}
输出:
"Hello Python World, I am diving into the javascript world."
看,第一个 JavaScript
单词被替换为 Python
,尽管我们试图匹配小写 javascript
。这是因为 i
标志,但它也在第一场比赛后停止。
现在,如果我们有多个句子或字符串怎么办?我们使用 g
修饰符来执行全局匹配并替换所有匹配的单词/模式。
示例代码:
let p =
'Hello JavaScript World, I am diving into the javascript world. It was good to learn JavaScript.'
const pattern = /JavaScript/g;
if (pattern.test(pattern)) {
let result = p.replace(pattern, 'Python');
console.log(result);
}
输出:
"Hello Python World, I am diving into the javascript world. It was good to learn Python."
请记住,g
标志进行区分大小写的匹配。这就是为什么在上面的输出中没有替换 javascript
。
如果我们也想替换它,我们可以使用带有 g
标志的 i
标志。看看下面的代码。
let p =
'Hello JavaScript World, I am diving into the javascript world. It was good to learn JavaScript.'
const pattern = /JavaScript/gi;
if (pattern.test(pattern)) {
let result = p.replace(pattern, 'Python');
console.log(result);
}
输出:
"Hello Python World, I am diving into the Python world. It was good to learn Python."
如果我们有一个字符串分布在多行上怎么办?在这里,m
标志进入动作并进行多行匹配。
m
标志不仅使用 ^
在开头匹配,使用 $
符号匹配每行的末尾,还可以匹配一行的开始/结束。
示例代码:
let p = `1.JavaScript\n
2.Python
3.Java.`
const pattern = /^\d/mg;
if (p.match(pattern)) {
let result = p.replace(pattern, 'ListItem');
console.log(result);
}
输出:
ListItem.JavaScript
ListItem.Python
ListItem.Java.
如果我们从上面的示例中删除 m
标志,它只会替换第一行的模式(参见下面的代码)。
let p = `1.JavaScript\n
2.Python
3.Java.`
const pattern = /^\d/g;
if (p.match(pattern)) {
let result = p.replace(pattern, 'ListItem');
console.log(result);
}
输出:
ListItem.JavaScript
2.Python
3.Java.
使用下面的示例代码,我们可以看到还可以在每一行的末尾找到模式并将它们替换为所需的值。
let p = `1.JavaScript 1\n
2.Python 2
3.Java 3`;
const pattern = /\d$/gm;
if (p.match(pattern)) {
let result = p.replace(pattern, 'ListItem');
console.log(result);
}
输出:
1.JavaScript ListItem
2.Python ListItem
3.Java ListItem
到目前为止,我们已经研究了正则表达式的字面表示法。当我们知道模式将保持不变时使用此表示法。
否则,如果我们怀疑模式会改变,最好使用构造函数。以下是构造函数的示例。
let p =
'Hello JavaScript World, I am diving into the javascript world. It was good to learn JavaScript.';
const pattern = new RegExp('\javascript', 'gi');
console.log(p.replace(pattern, 'Python'));
输出:
"Hello Python World, I am diving into the Python world. It was good to learn Python."
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
用 jQuery 检查复选框是否被选中
发布时间:2024/03/24 浏览次数:102 分类:JavaScript
-
在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的
jQuery 中的 Window.onload 与 $(document).ready
发布时间:2024/03/24 浏览次数:180 分类:JavaScript
-
本教程演示了如何在 jQuery 中使用 Window.onload 和 $(document).ready 事件。