Home > Web Front-end > JS Tutorial > body text

A brief discussion on the replace() method in javascript

高洛峰
Release: 2017-01-04 13:36:14
Original
1052 people have browsed it

定义和用法
replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。
语法
stringObject.replace(regexp/substr,replacement)

A brief discussion on the replace() method in javascript

返回值
一个新的字符串,是用 replacement 替换了 regexp 的第一次匹配或所有匹配之后得到的。
说明
字符串 stringObject 的 replace() 方法执行的是查找并替换的操作。它将在 stringObject 中查找与 regexp 相匹配的子字符串,然后用 replacement 来替换这些子串。如果 regexp 具有全局标志 g,那么 replace() 方法将替换所有匹配的子串。否则,它只替换第一个匹配子串。

replacement 可以是字符串,也可以是函数。如果它是字符串,那么每个匹配都将由字符串替换。但是 replacement 中的 $ 字符具有特定的含义。如下表所示,它说明从模式匹配得到的字符串将用于替换。

A brief discussion on the replace() method in javascript

注意:ECMAScript v3 规定,replace() 方法的参数 replacement 可以是函数而不是字符串。在这种情况下,每个匹配都调用该函数,它返回的字符串将作为替换文本使用。该函数的第一个参数是匹配模式的字符串。接下来的参数是与模式中的子表达式匹配的字符串,可以有 0 个或多个这样的参数。接下来的参数是一个整数,声明了匹配在 stringObject 中出现的位置。最后一个参数是 stringObject 本身。
实例
例子 1
在本例中,我们将使用 "W3School" 替换字符串中的 "Microsoft":

<script type="text/javascript">
 
var str="Visit Microsoft!"
document.write(str.replace(/Microsoft/, "php"))
 
</script>
Copy after login

输出:Visit php!
例子 2
在本例中,我们将执行一次全局替换,每当 "Microsoft" 被找到,它就被替换为 "W3School":

<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, "php"))
 
</script>
Copy after login

输出:
Welcome to php! We are proud to announce that jb1

has one of the largest Web Developers sites in the world.
例子 3
您可以使用本例提供的代码来确保匹配字符串大写字符的正确:

text = "javascript Tutorial";
text.replace(/javascript/i, "JavaScript");
Copy after login

输出:javascript Tutorial
例子 4
在本例中,我们将把 "Doe, John" 转换为 "John Doe" 的形式:

name = "Doe, John";
name.replace(/(\w+)\s*, \s*(\w+)/, "$2 $1");
Copy after login

输出:John Doe
例子 5
在本例中,我们将把所有的花引号替换为直引号:

name = &#39;"a", "b"&#39;;
name.replace(/"([^"]*)"/g, "&#39;$1&#39;");
Copy after login

输出:'a', 'b'
例子 6
在本例中,我们将把字符串中所有单词的首字母都转换为大写:

name = &#39;aaa bbb ccc&#39;;
uw=name.replace(/\b\w+\b/g, function(word){
 return word.substring(0,1).toUpperCase()+word.substring(1);}
 );
Copy after login

输出:Aaa Bbb Ccc

通过本文简单的学习,大家应该大概知道javascript中replace()方法,结合实例练习,希望对大家的学习有所帮助。

更多A brief discussion on the replace() method in javascript相关文章请关注PHP中文网!


Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!