


Tips for using the character replacement function String.replace() in js_javascript tips
定义和用法
replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。
语法
stringObject.replace(regexp/substr,replacement)参数 描述
regexp/substr 必需。规定子字符串或要替换的模式的 RegExp 对象。
请注意,如果该值是一个字符串,则将它作为要检索的直接量文本模式,而不是首先被转换为 RegExp 对象。
replacement 必需。一个字符串值。规定了替换文本或生成替换文本的函数。
返回值
一个新的字符串,是用 replacement 替换了 regexp 的第一次匹配或所有匹配之后得到的。
说明
字符串 stringObject 的 replace() 方法执行的是查找并替换的操作。它将在 stringObject 中查找与 regexp 相匹配的子字符串,然后用 replacement 来替换这些子串。如果 regexp 具有全局标志 g,那么 replace() 方法将替换所有匹配的子串。否则,它只替换第一个匹配子串。
replacement 可以是字符串,也可以是函数。如果它是字符串,那么每个匹配都将由字符串替换。但是 replacement 中的 $ 字符具有特定的含义。如下表所示,它说明从模式匹配得到的字符串将用于替换。
字符 替换文本
$1、$2、...、$99 与 regexp 中的第 1 到第 99 个子表达式相匹配的文本。
$& 与 regexp 相匹配的子串。
$` 位于匹配子串左侧的文本。
$' 位于匹配子串右侧的文本。
$$ 直接量符号。
注意:ECMAScript v3 规定,replace() 方法的参数 replacement 可以是函数而不是字符串。在这种情况下,每个匹配都调用该函数,它返回的字符串将作为替换文本使用。该函数的第一个参数是匹配模式的字符串。接下来的参数是与模式中的子表达式匹配的字符串,可以有 0 个或多个这样的参数。接下来的参数是一个整数,声明了匹配在 stringObject 中出现的位置。最后一个参数是 stringObject 本身。
实例
例子 1
在本例中,我们将使用 "jb51.net" 替换字符串中的 "Microsoft":
<script type="text/javascript"> var str="Visit Microsoft!" document.write(str.replace(/Microsoft/, "jb51.net")) </script>
输出:
Visit jb51.net!
例子 2
在本例中,我们将执行一次全局替换,每当 "Microsoft" 被找到,它就被替换为 "jb51.net":
<script type="text/javascript"> var str="Welcome to Microsoft! " str=str + "We are proud to announce that Microsoft has " str=str + "one of the largest Web Developers sites in the world." document.write(str.replace(/Microsoft/g, "jb51.net")) </script>
输出:
Welcome to jb51.net! We are proud to announce that jb51.net
has one of the largest Web Developers sites in the world.
例子 3
您可以使用本例提供的代码来确保匹配字符串大写字符的正确:
text = "javascript Tutorial"; text.replace(/javascript/i, "JavaScript");
例子 4
在本例中,我们将把 "Doe, John" 转换为 "John Doe" 的形式:
name = "Doe, John"; name.replace(/(\w+)\s*, \s*(\w+)/, "$2 $1");
例子 5
在本例中,我们将把所有的花引号替换为直引号:
name = '"a", "b"'; name.replace(/"([^"]*)"/g, "'$1'");
例子 6
在本例中,我们将把字符串中所有单词的首字母都转换为大写:
name = 'aaa bbb ccc'; uw=name.replace(/\b\w+\b/g, function(word){ return word.substring(0,1).toUpperCase()+word.substring(1);} );
String.replace( ) Introduction
Syntax:
var strings = string.replace(regexp, replacement)
regexp: The regular expression you want to perform the replacement operation. If a string is passed in, it will be treated as an ordinary character, and only one replacement operation will be performed; if it is a regular expression, and it contains global ( g) modifier, all occurrences of the target character will be replaced, otherwise, only one replacement operation will be performed.
replacement: The character you want to replace with.
The return value is the string after performing the replacement operation.
11 Simple usage of String.replace()
var text = "javascript is very powerful!";13 text.replace(/javascript/i, "JavaScript");14 // Return: JavaScript is very powerful!
String.replace( ) replaces all occurrences of the target character
var text= "javascript is very powerful! JAVASCRIPT is my favorite language!";17 text.replace(/javascript/ig, "JavaScript");18 // Return: JavaScript is very powerful! JavaScript is my favorite language!
String.replace() implements swapping positions
var name= "Doe, John";
name.replace(/(w )s*,s*(w )/, "$2 $1");
// Return: John Doe
String.replace() replaces all characters contained in double quotes with characters contained in square brackets
var text = '"JavaScript" is very powerful! ';25 text.replace(/"([^"]*)"/g, "[$1]");26 // Return: [JavaScript] Very powerful!
String.replace( ) capitalizes the first letter of all characters
var text = 'a journey of a thousand miles begins with single step.';29 text.replace(/bw b/g, function(word) {30 return word.substring(0,1).toUpperCase( ) 31 word. substring(1);32 });33 34 // Return: A Journey Of A Thousand Miles Begins With Single Step.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Convert basic data types to strings using Java's String.valueOf() function In Java development, when we need to convert basic data types to strings, a common method is to use the valueOf() function of the String class. This function can accept parameters of basic data types and return the corresponding string representation. In this article, we will explore how to use the String.valueOf() function for basic data type conversions and provide some code examples to

Method of converting char array to string: It can be achieved by assignment. Use {char a[]=" abc d\0efg ";string s=a;} syntax to let the char array directly assign a value to string, and execute the code to complete the conversion.

Replace characters (strings) in a string using Java's String.replace() function In Java, strings are immutable objects, which means that once a string object is created, its value cannot be modified. However, you may encounter situations where you need to replace certain characters or strings in a string. At this time, we can use the replace() method in Java's String class to implement string replacement. The replace() method of String class has two types:

Hello everyone, today I will share with you the basic knowledge of Java: String. Needless to say the importance of the String class, it can be said to be the most used class in our back-end development, so it is necessary to talk about it.

Use Java's String.length() function to get the length of a string. In Java programming, string is a very common data type. We often need to get the length of a string, that is, the number of characters in the string. In Java, we can use the length() function of the String class to get the length of a string. Here is a simple example code: publicclassStringLengthExample{publ

In Golang programming, byte, rune and string types are very basic and common data types. They play an important role in processing data operations such as strings and file streams. When performing these data operations, we usually need to convert them to each other, which requires mastering some conversion skills. This article will introduce the byte, rune and string type conversion techniques of Golang functions, aiming to help readers better understand these data types and be able to apply them skillfully in programming practice.

MySQL is a commonly used relational database management system that provides a variety of functions to process and operate data. Among them, the REPLACE function is used to replace the specified part of the string. In this article, we will introduce how to use the REPLACE function for string replacement in MySQL and demonstrate its usage through code examples. First, let’s take a look at the syntax of the REPLACE function: REPLACE(str,search_str,replace_str).

1. Understanding String1. String in JDK First, let’s take a look at the source code of the String class in the JDK. It implements many interfaces. You can see that the String class is modified by final. This means that the String class cannot be inherited and there is no subclass of String. class, so that all people using JDK use the same String class. If String is allowed to be inherited, everyone can extend String. Everyone uses different versions of String, and two different people Using the same method shows different results, which makes it impossible to develop the code. Inheritance and method overriding not only bring flexibility, but also cause many subclasses to behave differently.
