javascript的 replace() 方法的使用讲解

String.prototype.replace()

The replace() method returns a new string with some or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match.语法:  str.replace(regexp|substr, newSubStr|function[, flags])

Parameters

regexp (pattern)
RegExp object or literal. The match is replaced by the return value of parameter #2.
substr (pattern)
String that is to be replaced by newSubStr. It is treated as a verbatim string and is notinterpreted as a regular expression.
newSubStr (replacement)
The String that replaces the substring received from parameter #1. A number of special replacement patterns are supported; see the "Specifying a string as a parameter" section below.
function (replacement)
A function to be invoked to create the new substring (to put in place of the substring received from parameter #1). The arguments supplied to this function are described in the "Specifying a function as a parameter" section below.
例子:
例1:

var str = ‘Twas the night before Xmas...‘;
var newstr = str.replace(/xmas/i, ‘Christmas‘);
console.log(newstr);  // Twas the night before Christmas...

例2:

var re = /apples/gi;
var str = ‘Apples are round, and apples are juicy.‘;
var newstr = str.replace(re, ‘oranges‘);
console.log(newstr);  // oranges are round, and oranges are juicy.

例3:

var re = /(\w+)\s(\w+)/;
var str = ‘John Smith‘;
var newstr = str.replace(re, ‘$2, $1‘);
console.log(newstr);  // Smith, Joh

例4:

function styleHyphenFormat(propertyName) {
  function upperToHyphenLower(match) {
    return ‘-‘ + match.toLowerCase();
  }
  return propertyName.replace(/[A-Z]/g, upperToHyphenLower);
}

例5:

function replacer(match, p1, p2, p3, offset, string) {
  // p1 is nondigits, p2 digits, and p3 non-alphanumerics
  console.log(arguments);
  return [p1, p2, p3].join(‘ - ‘);
}
var newString = ‘abc12345#$*%aaa‘.replace(/([^\d]*)(\d*)([^\w]*)/, replacer);

  

时间: 2024-08-11 03:28:25

javascript的 replace() 方法的使用讲解的相关文章

JavaScript中replace()方法的第二个参数解析

语法 string.replace(searchvalue,newvalue) 参数值 searchvalue 必须.规定子字符串或要替换的模式的 RegExp 对象.请注意,如果该值是一个字符串,则将它作为要检索的直接量文本模式,而不是首先被转换为 RegExp 对象. newvalue 必需.一个字符串值.规定了替换文本或生成替换文本的函数. 返回值 String 一个新的字符串,是用 replacement 替换了 regexp 的第一次匹配或所有匹配之后得到的. 第一个参数searchv

JS中的replace方法

JavaScript中replace() 方法如果直接用str.replace("-","!") 只会替换第一个匹配的字符. 而str.replace(/\-/g,"!")则可以全部替换掉匹配的字符(g为全局标志). 所以可以用以下几种方式.:string.replace(/reallyDo/g, replaceWith);string.replace(new RegExp(reallyDo, 'g'), replaceWith); string

js的replace方法

今天在项目中发现,js的replace方法,其实只是替换第一个匹配的字符: 比如 backstreetboy.replace('b','B') 得到的结果是Backstreetboy,只是替换了第一个匹配的字符,要想全部替换需要用正则表达式来替换,backstreetboy.replace(/b/g,'B'),这样就会全部替换了. ********************下面是从网上搜索到的资料********************** 第一次发现JavaScript中replace() 方法

JavaScript 中的 replace 方法

定义和用法 replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. stringObject.replace(regexp/substr,replacement) 参数 描述 regexp/substr 必需.规定子字符串或要替换的模式的 RegExp 对象. 请注意,如果该值是一个字符串,则将它作为要检索的直接量文本模式,而不是首先被转换为 RegExp 对象. replacement 必需.一个字符串值.规定了替换文本或生成替换文本的函数. 返回值

深入理解 JavaScript 中的 replace 方法(转)

replace方法是属于String对象的,可用于替换字符串. 简单介绍: StringObject.replace(searchValue,replaceValue) StringObject:字符串 searchValue:字符串或正则表达式 replaceValue:字符串或者函数 字符串替换字符串 JavaScript 1 'I am loser!'.replace('loser','hero')//I am hero! 直接使用字符串能让自己从loser变成hero,但是如果有2个lo

JavaScript replace() 方法

定义和用法 replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. stringObject.replace(regexp/substr,replacement) 参数 描述 regexp/substr 必需.规定子字符串或要替换的模式的 RegExp 对象. 请注意,如果该值是一个字符串,则将它作为要检索的直接量文本模式,而不是首先被转换为 RegExp 对象. replacement 必需.一个字符串值.规定了替换文本或生成替换文本的函数. 返回值

javascript的replace之正则表达式的浅析

在javascript中,字符串的replace方法可以指定替换某些字符串. 1.直接替换字符串 "yy/MM/dd".replace("yy","2017");//替换后,原字符串变为2017/MM/dd 这是直接指定将原字符串中的yy替换为2017. 2.指定用函数返回值替换原指定字符串 "yy/MM/dd".replace("yy",function(){return "2017"

JavaScript中trim 方法实现

Java中的 String 类有个trim() 可以删除字符串前后的空格字符,jQuery中也有trim()方法可以删除字符变量前后的字符串.但是JavaScript中却没有相应的trim() 方法,幸好,JavaScript中有正则表达式,String 对象有replace() 方法,利用JavaScript的正则和replace方法来达到trim() 方法的效果. 接下来介绍两种方法,其实两种方法大同小异.都是在String 对象的prototype属性上进行trim()方法的定义,并提供实

Javascript变量与方法

变量命名 变量必须以字母开头 变量也能以 $ 和 _ 符号开头(私有变量命名常用"_"开头) 变量名称对大小写敏感 变量声明 通过var显式声明(var user),若不声明,则为全局变量,即为window对象的属性,可通过window.user获取,其值为undefined.也可在一条语句中声明多个变量 var a=1, b=2, c=3; 变量可以重复声明 Javascript中,作用域是由函数划分的,而不是由块划分(如while, if, for).不过代码总要有一个上下文对象,