Home  >  Article  >  Web Front-end  >  Tips for using the character replacement function String.replace() in js_javascript tips

Tips for using the character replacement function String.replace() in js_javascript tips

WBOY
WBOYOriginal
2016-05-16 18:03:341084browse

定义和用法

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.

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