javascript的 replace() 方法的使用讲解
String.prototype.replace()
Thereplace()method returns a new string with some or all matches of apatternreplaced by areplacement.
Thepatterncan be a string or aRegExp, and thereplacementcan be a string or a function to be called for each match.
语法:
str.replace(regexp|substr, newSubStr|function[, flags])
Parameters
regexp (pattern)- A
RegExpobject or literal. The match is replaced by the return value of parameter #2. substr (pattern)- A
Stringthat is to be replaced bynewSubStr. It is treated as a verbatim string and is notinterpreted as a regular expression. newSubStr (replacement)- The
Stringthat 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);
javascript的 replace() 方法的使用讲解的更多相关文章
- JavaScript的replace方法与正则表达式结合应用讲解
大家好!!今晚在华软G43*宿舍没什么事做,把javascript中replace方法讲解一下,如果讲得不对或不合理是情理之中的事,因为我不是老鸟,也不是菜鸟,我也不知道我当底是什么鸟??呵~~ re ...
- JavaScript 中 replace方法 替换所有字符串
需要替换一个字符串中所有的某个字符串 java中使用replaceAll()方法就可以了.但是JavaScript中没有replaceAll方法 但是可以通过以下方法实现: /** * 空格替换为下划 ...
- JavaScript stringObject.replace() 方法
定义和用法: replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. 语法: stringObject.replace(RegExp/substr,reol ...
- javascript中replace( )方法的使用——有博主已经讲过了,但里面有一小丢丢知识错误,挺重要的部分,我就重提下,以免初学者弄错
阿里面试题:说出以下函数的作用是?空白区域应该填写什么? 其实这个问题http://www.phpstudy.net/b.php/105983.html解释的已经非常好了,思路也很顺,容易理解,本文将 ...
- javascript的replace方法的高级应用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- JavaScript中replace()方法的第二个参数解析
语法 string.replace(searchvalue,newvalue) 参数值 searchvalue 必须.规定子字符串或要替换的模式的 RegExp 对象.请注意,如果该值是一个字符串,则 ...
- js的replace方法
今天在项目中发现,js的replace方法,其实只是替换第一个匹配的字符: 比如 backstreetboy.replace('b','B') 得到的结果是Backstreetboy,只是替换了第一个 ...
- JS中的replace方法
JavaScript中replace() 方法如果直接用str.replace("-","!") 只会替换第一个匹配的字符. 而str.replace(/\-/ ...
- JavaScript 中的 replace 方法
定义和用法 replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串. stringObject.replace(regexp/substr,replaceme ...
随机推荐
- AngularJs多重视图和路由的使用
使用AngularJs来做多重视图和路由是在方便不过了,在开发过程中,都有许多的页面,而这些页面都有相同的部分,比如页面的头部和尾部通常都是一样的,变化的都是主体部分,还有就是一些后端管理的一些项目, ...
- 解决Xcode 9.2系统真机测试时出现 could not find developer disk image问题
解决Xcode在ipad/iphone 9.2 系统真机测试时出现could not find developer disk image问题 第一种方法:拷贝这个文件(http://download. ...
- isPostBack原理
从 到输入用户名,点击提交按钮 这个过程就叫做postback(是两个不同的状态) 利用ispostback原理,实现是否第一次进入处理程序(上一个用用户名判断的不好,会导致在用户名空的情况下 ...
- weblogic 密码加密
先启动域:
- SQL语句创建access表
CREATE TABLE Persons(ID AutoIncrement primary key,Id_P int NOT NULL,LastName varchar(255) NOT NULL,s ...
- 【sort】 基数排序
下面这段问答摘自csdn: 把基数排序说成桶排序应该是没有太大问题的.总的说来,应该把这一类归为分配排序,由于分配排序的一些缺陷,主要是时间代价很差,改进成为桶式排序(bucket sort),而桶排 ...
- C: define many functions using predefine..
/* Defines COUNTER. There must be exactly one such definition at file scope * within a program. */ # ...
- 判断Android应用是否安装、运行
本文介绍3个方法,判断手机上是否安装了某应用.该应用是否正在运行.获取手机某个APK文件的安装Intent.启动该Intent就可以直接安装该APK. /** * 判断应用是否已安装 * * @par ...
- Talking about Health
George: Doctor, what is the best way to stay healthy? Dictor: Having a good diet is probably the mos ...
- Chapter 1 First Sight——15
The red-haired woman looked up. "Can I help you?" 红头发的女人抬头看了一眼说,有什么我能帮助你的吗? "I'm Isab ...