Home  >  Article  >  Web Front-end  >  Is javascript replace() case-insensitive?

Is javascript replace() case-insensitive?

青灯夜游
青灯夜游Original
2021-11-04 15:37:325617browse

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.

Is javascript replace() case-insensitive?

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.

Special characters in the second parameter of the replace() method
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 , &#39;x&#39;);
document.getElementById("demo").innerHTML = txt;
</script>

输出结果:

Is javascript replace() case-insensitive?

示例2

下面代码把字符串中每个单词转换为首字母大写形式显示。

var s = &#39;javascript is script , is not java.&#39;;  //定义字符串
//定义替换文本函数,参数为第一个子表达式匹配文本
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 = &#39;javascript is script , is not java.&#39;;  //定义字符串
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 = &#39;javascript is script , is not java.&#39;;  //定义字符串
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!

Statement:
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