Table of Contents
替换单个字串
替换多个子串
替换特殊字符
Home Web Front-end JS Tutorial How to replace strings in JavaScript? 3 methods introduced

How to replace strings in JavaScript? 3 methods introduced

Nov 02, 2020 pm 05:49 PM
javascript string

How to replace strings in JavaScript? 3 methods introduced

替换字符串中的文本是 JavaScript 中的常见任务。本文研究几种用 replace 和正则表达式替换文本的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

替换单个字串

通常 JavaScript 的 String replace() 函数只会替换它在字符串中找到的第一个匹配的子符:

1

2

3

const myMessage = 'this is the sentence to end all sentences';

const newMessage = myMessage.replace('sentence''message');

console.log(newMessage); // this is the message to end all sentences

Copy after login

在这个例子中,仅替换了第一个 sentence 字串。

替换多个子串

如果希望 JavaScript 能够替换所有子串,必须通过 /g 运算符使用正则表达式:

1

2

3

const myMessage = 'this is the sentence to end all sentences';

const newMessage = myMessage.replace(/sentence/g, 'message');

console.log(newMessage); // this is the message to end all messages

Copy after login

这一次次两个子串都会被替换。

除了使用内联 /g 之外,还可以使用 RegExp 对象的构造函数:

1

2

3

const myMessage = 'this is the sentence to end all sentences';

const newMessage = myMessage.replace(new RegExp('sentence''g'), 'message');

console.log(newMessage); // this is the message to end all messages"

Copy after login

替换特殊字符

要替换特殊字符,例如 -/\\^$*+?.()|[]{}),需要使用反斜杠对其转义。

如果给定字符串 this\\-is\\-my\\-url,要求把所有转义的减号( \\-)替换为未转义的减号(-)。

可以用 replace() 做到:

1

2

3

const myUrl = 'this\-is\-my\-url';

const newUrl = myMessage.replace(/\\-/g, '-');

console.log(newUrl); // this-is-my-url

Copy after login

或者用new Regexp()

1

2

3

const myUrl = 'this\-is\-my\-url';

const newUrl = myUrl.replace(new RegExp('\-''g'), '-');

console.log(newUrl); // this-is-my-url

Copy after login

在第二个例子中不必用反斜杠来转义反斜杠。

你还知道哪些方法,请留言告诉大家。

相关免费学习推荐:js视频教程

更多编程相关知识,请访问:编程入门!!

The above is the detailed content of How to replace strings in JavaScript? 3 methods introduced. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of the method of converting int type to string in PHP Detailed explanation of the method of converting int type to string in PHP Mar 26, 2024 am 11:45 AM

Detailed explanation of the method of converting int type to string in PHP

How to determine whether a Golang string ends with a specified character How to determine whether a Golang string ends with a specified character Mar 12, 2024 pm 04:48 PM

How to determine whether a Golang string ends with a specified character

How to repeat a string in python_python repeating string tutorial How to repeat a string in python_python repeating string tutorial Apr 02, 2024 pm 03:58 PM

How to repeat a string in python_python repeating string tutorial

How to check if a string starts with a specific character in Golang? How to check if a string starts with a specific character in Golang? Mar 12, 2024 pm 09:42 PM

How to check if a string starts with a specific character in Golang?

How to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP How to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP Mar 04, 2024 am 09:36 AM

How to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP

PHP String Matching Tips: Avoid Ambiguous Included Expressions PHP String Matching Tips: Avoid Ambiguous Included Expressions Feb 29, 2024 am 08:06 AM

PHP String Matching Tips: Avoid Ambiguous Included Expressions

PHP string manipulation: a practical way to effectively remove spaces PHP string manipulation: a practical way to effectively remove spaces Mar 24, 2024 am 11:45 AM

PHP string manipulation: a practical way to effectively remove spaces

PHP techniques for deleting the last two characters of a string PHP techniques for deleting the last two characters of a string Mar 23, 2024 pm 12:18 PM

PHP techniques for deleting the last two characters of a string

See all articles