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 ...
随机推荐
- 5.如果第4题中在DOS命令下输入:java Hello 出现以下结果:Bad command or the file name 可能是什么原因?请说明理由。
1.没有输入javac指定路径,而是直接输入java Hello. 2.前面已经用过一次指令,没有重新输入路径.
- listview解决滑动条目的时候背景变为黑色的问题
方式一:java代码: listView.setCacheColorHint(0); 方式二:布局文件 <ListView android:id="@+id/listView1&quo ...
- [code]判断周期串
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #include<stdio.h> #include<string.h> ...
- 使用固件库操作STM32F4时的必要配置(转)
源:使用固件库操作STM32F4时的必要配置 使用STM32F4的固件库时,默认的晶振为25Mhz晶振,因此需要做一定的修改.之前因为一直没有注意这个问题,我捣腾了许久,发现工作时钟总是不对,查阅了一 ...
- STM32F407的硬件I2C
源:STM32F407的硬件I2C 我使用的是STM32的固件库. 硬件模块使用之前必须配置其参数,I2C的配置如下: void IIC_Config(void) { GPIO_InitTypeDef ...
- iabtis初探
1.简介 与Hibernate相比,ibatis属于一种半自动的ORM框架,主要解决了java对象与SQL入参及结果集的映射关系.简单易学.容易上手:但是安全性较差,对于金融等对安全要求较高的系统来说 ...
- java 单例模式及getInstance的好处
1.什么是单例模式 简单理解为,有一个类,只能有一个实例化对象,这就是单例模式. 2.getInstance的好处 首先看一下怎样使用getInstance实现单例模式 public class Co ...
- java设计模式案例详解:代理模式
代理模式就是用一个第三者的身份去完成工作,其实际意义跟字面意思其实是一样的,理解方式有很多,还是例子直观. 本例的实现类是实现买票功能,实际应用想要添加身份验证功能,利用代理模式添加验证步骤.上例子: ...
- Anton and Chess
Anton and Chess time limit per test 4 seconds memory limit per test 256 megabytes input standard inp ...
- Android Camera(一)
最近老大交给了一个任务,说是要在本地视频端很够调节摄像头焦距. 碰到了一些问题: 1.手机支不支持摄像头变焦 2.系统自带摄像软件可以变焦,但是自己编写的程序不支持变焦, 这个问题网上也有很多童鞋碰到 ...