js中字符串的替换

定义和用法

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 本身。
实例

i、replace方法

该方法的作用是替换字符串中所有指定的字符,然后生成一个新的字符串。经过该方法调用以后,原来的字符串不发生改变。例如:

String s = “abcat”;

String s1 = s.replace(‘a’,‘1’);

该代码的作用是将字符串s中所有的字符a替换成字符1,生成的新字符串s1的值是“1bc1t”,而字符串s的内容不发生改变。

如果需要将字符串中某个指定的字符串替换为其它字符串,则可以使用replaceAll方法,例如:

String s = “abatbac”;

String s1 = s.replaceAll(“ba”,“12”);

该代码的作用是将字符串s中所有的字符串“ab”替换为“12”,生成新的字符串“a12t12c”,而字符串s的内容也不发生改变。

如果只需要替换第一个出现的指定字符串时,可以使用replaceFirst方法,例如:

String s = “abatbac”;

String s1 = s. replaceFirst (“ba”,“12”);

该代码的作用是只将字符串s中全局出现的字符串“,”替换为字符串“/”。

var str = “Is,is,the,cost,of,of,gasoline,going,up,up”;

document.write(str.replace(/\,/g,’/‘));

运用正则进行全局匹配将全局的“,”替换为“/”。

*例子 1
*
在本例中,我们将使用 “jb51.net” 替换字符串中的 “Microsoft”:

?

1

2

3

4

<code class="js string">"text/javascript"</code><code class="js plain">></code></div> <div class="line number2 index1 alt1"><code class="js keyword">var</code> <code class="js plain">str=</code><code class="js string">"Visit Microsoft!"</code></div> <div class="line number3 index2 alt2"><code class="js plain">document.write(str.replace(/Microsoft/, </code><code class="js string">"jb51.net"</code><code class="js plain">))</code></div> <div class="line number4 index3 alt1"><code class="js plain">

输出:

Visit jb51.net!

*例子 2
*
在本例中,我们将执行一次全局替换,每当 “Microsoft” 被找到,它就被替换为 “jb51.net”:

?

1

2

3

4

5

6

<code class="js string">"text/javascript"</code><code class="js plain">></code></div> <div class="line number2 index1 alt1"><code class="js keyword">var</code> <code class="js plain">str=</code><code class="js string">"Welcome to Microsoft! "</code></div> <div class="line number3 index2 alt2"><code class="js plain">str=str + </code><code class="js string">"We are proud to announce that Microsoft has "</code></div> <div class="line number4 index3 alt1"><code class="js plain">str=str + </code><code class="js string">"one of the largest Web Developers sites in the world."</code></div> <div class="line number5 index4 alt2"><code class="js plain">document.write(str.replace(/Microsoft/g, </code><code class="js string">"jb51.net"</code><code class="js plain">))</code></div> <div class="line number6 index5 alt1"><code class="js plain">

输出:

Welcome to jb51.net! We are proud to announce that jb51.net
has one of the largest Web Developers sites in the world.

*例子 3
*
您可以使用本例提供的代码来确保匹配字符串大写字符的正确:

?

1

2

text = "javascript Tutorial"``;

text.replace(/javascript/i, "JavaScript"``);

*例子 4
*
在本例中,我们将把 “Doe, John” 转换为 “John Doe” 的形式:

?

1

2

name = "Doe, John"``;

name.replace(/(\w+)\s*, \s*(\w+)/, "$2 $1"``);

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

?

1

2

name = '"a", "b"'``;

name.replace(/``"([^"``]*)``"/g, "``'$1'``");

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

?

1

2

3

4

name = 'aaa bbb ccc'``;

uw=name.replace(/\b\w+\b/g, function``(word){

return word.substring(0,1).toUpperCase()+word.substring(1);}

);
$$


文章作者: xkloveme
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 xkloveme !
评论
 上一篇
常用正则表达式总结 常用正则表达式总结
一、校验数字的表达式 数字:^[0-9]\*$ n 位的数字:^\d{n}$ 至少 n 位的数字:^\d{n,}$ m-n 位的数字:^\d{m,n}$ 零和非零开头的数字:^(0|[1-9][0-9]\*)$ 非零开头的最多带两位小数的数
2017-09-19
下一篇 
vue实现非路由跳转以及数据传递 vue实现非路由跳转以及数据传递
定义一个父组件 /_ ============ * Home Index Page * ============ * * The home index page._/ import VLayout from ‘@/layouts/Defa
2017-09-16
  目录