Is javascript replace() case-insensitive?
In JavaScript, the replace() function can use regular expressions to replace strings in a case-insensitive manner, the syntax is "string.replace(/value to be found/gi,"replacement value")"; Where "g" stands for global replacement and "i" stands for ignoring case.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
In JavaScript, the replace() function can replace strings in a case-insensitive manner, which requires the help of regular expressions.
Thereplace() method is used to replace some characters with other characters in a string, or replace a substring that matches a regular expression.
Syntax:
string.replace(searchvalue,newvalue)
Parameters | Description |
---|---|
searchvalue | is required. A RegExp object that specifies the substring or pattern to be replaced. Note that if the value is a string, it is retrieved as a literal literal pattern rather than first being converted to a RegExp object. |
newvalue | Required. A string value. Specifies functions for replacing text or generating replacement text. |
Return value: a new string obtained by replacing the first match or all matches of regexp with replacement. The second parameter of the
replace() method can use a function. The function will be called when there is a match. The return value of the function will be used as the replacement text. At the same time, the function can receive $
as the second parameter. A prefix of special characters used to reference information related to the matched text.
Conventional string | Description |
---|---|
$1, $2, ..., $99 | Text matching the 1st to 99th subexpressions in the regular expression |
$& (Dollar sign hyphen) | Substring matching the regular expression |
$' (Dollar sign toggle skill key) | is located Matches the text on the left side of the substring |
$' (dollar sign single quote) | Matches the text on the right side of the string |
$$ | means $ string |
示例1
将字符串中的字符 a(不区分大小写) 替换为 x
<p> 将字符串中的字符 a(不区分大小写) 替换为 x </p> <p id="demo"></p> <script> var sText = "abcdefaABC"; //g 代表全局替换 i 代表 忽略大小写 var txt = sText.replace( /a/gi , 'x'); document.getElementById("demo").innerHTML = txt; </script>
输出结果:
示例2
下面代码把字符串中每个单词转换为首字母大写形式显示。
var s = 'javascript is script , is not java.'; //定义字符串 //定义替换文本函数,参数为第一个子表达式匹配文本 var f = function ($1) { //把匹配文本的首字母转换为大写 return $1.substring(0,1).toUpperCase() + $1.substring(1).toLowerCase();} var a = s.replace(/(\b\w+\b)/g, f); //匹配文本并进行替换 console.log(a); //返回字符串“JavaScript Is Script , Is Not Java.”
在上面示例中替换函数的参数为特殊字符“$1”,它表示正则表达式 /(\b\w+\b)/ 中小括号匹配的文本,然后在函数结构内对这个匹配文本进行处理,截取其首字母并转换为大写形式,余下字符全为小写,然后返回新处理的字符串。replace() 方法是在原文本中使用这个返回的新字符串替换掉每次匹配的子字符串。
示例3
对于上面的示例还可以进一步延伸,使用小括号来获取更多匹配信息。例如,直接利用小括号传递单词的首字母,然后进行大小写转换处理,处理结果都是一样的。
var s = 'javascript is script , is not java.'; //定义字符串 var f = function ($1,$2,$3) { //定义替换文本函数,请注意参数的变化 return $2.toUpperCase() + $3; } var a = s.replace(/(\b\w+\b)/g, f); console.log(a);
在函数 f() 中,第一个参数表示每次匹配的文本,第二个参数表示第一个小括号的子表达式所匹配的文本,即单词的首字母,第二个参数表示第二个小括号的子表达式所匹配的文本。
replace() 方法的第二个参数是一个函数,replace() 方法会给它传递多个实参,这些实参都包含一定的意思,具体说明如下:
第一个参数表示与匹配模式相匹配的文本,如上面示例中每次匹配的单词字符串。
其后的参数是与匹配模式中子表达式相匹配的字符串,参数个数不限,根据子表达式数而定。
后面的参数是一个整数,表示匹配文本在字符串中的下标位置。
最后一个参数表示字符串自身。
示例4
把上面示例中替换文本函数改为如下形式。
var f = function() { return arguments[1].toUpperCase() + arguments[2]; }
也就是说,如果不为函数传递形参,直接调用函数的 arguments 属性同样能够读取到正则表达式中相关匹配文本的信息。其中:
arguments[0]:表示每次匹配的文本,即单词。
arguments[1]:表示第一个子表达式匹配的文本,即单词的首个字母。
arguments[2]:表示第二个子表达式匹配的文本,即单词的余下字母。
arguments[3]:表示匹配文本的下标位置,如第一个匹配单词“javascript”的下标位置就是0,以此类推。
arguments[4]:表示要执行匹配的字符串,这里表示“javascript is script , is not java.”。
示例5
下面代码利用函数的 arguments 对象主动获取 replace() 方法的第一个参数中正则表达式所匹配的详细信息。
var s = 'javascript is script , is not java.'; //定义字符串 var f = function () { for (var i = 0; i < arguments.length; i++) { console.log("第" + (i + 1) + "个参数的值:"+ arguments[i]); } console.log("-----------------------------"); } var a = s.replace(/(\b\w+\b)/g, f);
在函数结构体中,使用 for 循环结构遍历 arguments 属性时,发现每次匹配单词时,都会弹出 5 次提示信息,分别显示上面所列的匹配文本信息。其中,arguments[1]、arguments[2] 会根据每次匹配文本的不同,分别显示当前匹配文本中子表达式匹配的信息,arguments[3] 显示当前匹配单词的下标位置。而 arguments[0] 总是显示每次匹配的单词,arguments[4] 总是显示被操作的字符串。
【推荐学习:javascript高级教程】
The above is the detailed content of Is javascript replace() case-insensitive?. For more information, please follow other related articles on the PHP Chinese website!

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











How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
