首页 web前端 js教程 JavaScript `string.replace()` useful cases

JavaScript `string.replace()` useful cases

Sep 13, 2024 pm 10:16 PM

JavaScript `string.replace()` useful cases

1. Simple String Replacement

Replace the first occurrence of a substring.

let str = "Hello world!";
let result = str.replace("world", "JavaScript");

// Output: "Hello JavaScript!"
登录后复制

2. Global String Replacement

Replace all occurrences of a substring, use the global (g) flag with regular expression.

let str = "Hello world, world!";
let result = str.replace(/world/g, "JavaScript");

// Output: "Hello JavaScript, JavaScript!"

登录后复制

3. Case-insensitive Replacement

You can make the replacement case-insensitive using the i flag.

let str = "Hello World, World!";
let result = str.replace(/world/gi, "JavaScript")

// Output: "Hello JavaScript, JavaScript!"
登录后复制

4. Replace Whole Words (Word Boundary)

Replace only whole words using \b word boundary.

let str = "This is a test word, test.";
let result = str.replace(/\btest\b/, "success");

// Output: "This is a success word, test."
登录后复制

Replace all occurrences of the whole word, use the global flag.

let str = "This is a test word, test.";
let result = str.replace(/\btest\b/g, "success");

// Output: "This is a success word, success."
登录后复制

5. Using a Function for Replacement

You can pass a function to replace() that dynamically generates the replacement string.

let str = "The price is $10";
let result = str.replace(/\$\d+/g, (match) => {
   return `$${parseInt(match.substring(1)) * 2}`
});

// Output: "The price is $20"
登录后复制

6. Capturing Groups with Replacement

Using regular expressions, you can capture parts of the match and reuse them in the replacement string.

let str = "John Smith";
let result = str.replace(/(\w+)\s(\w+)/, "$2, $1");

// Output: "Smith, John"
登录后复制

7. Escaping Special Characters

If you need to replace special characters like . or *, you need to escape them in the regular expression.

let str = "Price: 5.99";
let result = str.replace(/\./, ",");

// Output: "Price: 5,99"
登录后复制

8. Replacing Non-ASCII Characters

To replace characters that aren't in the ASCII range, you can use Unicode properties.

let str = "Héllo Wörld";
let result = str.replace(/[^\x00-\x7F]/g, "");

// Output: "Hllo Wrld"
登录后复制

9. Replacing Digits

You can replace digits (or groups of digits) using regular expressions.

let str = "Contact: 123-456-7890";
let result = str.replace(/\d{3}/g, "***");

// Output: "Contact: ***-***-***0"
登录后复制

10. Replacing Special Characters using Unicode

You can also use Unicode escape sequences to replace special characters.

let str = "I love ☕!";
let result = str.replace(/\u2615/g, "coffee"); 

// Output: "I love coffee!"
登录后复制

以上是JavaScript `string.replace()` useful cases的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门文章

仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
两个点博物馆:所有展览以及在哪里可以找到它们
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章

仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
两个点博物馆:所有展览以及在哪里可以找到它们
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章标签

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

在JavaScript中替换字符串字符 在JavaScript中替换字符串字符 Mar 11, 2025 am 12:07 AM

在JavaScript中替换字符串字符

jQuery检查日期是否有效 jQuery检查日期是否有效 Mar 01, 2025 am 08:51 AM

jQuery检查日期是否有效

jQuery获取元素填充/保证金 jQuery获取元素填充/保证金 Mar 01, 2025 am 08:53 AM

jQuery获取元素填充/保证金

10个jQuery手风琴选项卡 10个jQuery手风琴选项卡 Mar 01, 2025 am 01:34 AM

10个jQuery手风琴选项卡

10值得检查jQuery插件 10值得检查jQuery插件 Mar 01, 2025 am 01:29 AM

10值得检查jQuery插件

HTTP与节点和HTTP-Console调试 HTTP与节点和HTTP-Console调试 Mar 01, 2025 am 01:37 AM

HTTP与节点和HTTP-Console调试

jQuery添加卷轴到Div jQuery添加卷轴到Div Mar 01, 2025 am 01:30 AM

jQuery添加卷轴到Div

自定义Google搜索API设置教程 自定义Google搜索API设置教程 Mar 04, 2025 am 01:06 AM

自定义Google搜索API设置教程

See all articles