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 ...
 
随机推荐
- perl的logwrapper
			
来源: http://www.cnblogs.com/itech/archive/2012/09/22/2698385.html 对任何的函数将标准输出和错误输出重定向到对应的log文件. 对任何的函 ...
 - shell中 if else以及大于、小于、等于逻辑表达式介绍
			
比如比较字符串.判断文件是否存在及是否可读等,通常用"[]"来表示条件测试. 注意:这里的空格很重要.要确保方括号的空格.笔者就曾因为空格缺少或位置不对,而浪费好多宝贵的时间. i ...
 - sql数据库删除表的外键约束(INSERT 语句与 FOREIGN KEY 约束"XXX"冲突。该冲突发生于数据库"XXX",表"XXX", column 'XXX)
			
使用如下SQL语句查询出表中外键约束名称: 1 select name 2 from sys.foreign_key_columns f join sys.objects o on f.constra ...
 - MySQL整理碎片
			
1 innodb引擎表 alter table TABLE_NAME engine='innodb'; 还有一种方法 optiize table TABLE_NAME; http://stackove ...
 - ExceL转PDF
			
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Excel = ...
 - API CLOUD 快捷键
			
常用快捷键有:Ctrl+Z:撤销Ctrl+N:创建项目或文件Ctrl+Shift+F:代码格式化(这个经常用,可以美化代码,也可以通过这个检查代码是否出错)Ctrl+/ :注释和反注释Alt+/:强制 ...
 - Openlays  3 绘制基本图形
			
<body> <div id="menu"> <label>几何图形类型:</label> <select id=" ...
 - 取得GridView某行的DataKey
			
首先绑定DataKeyNames GridView.DataKeyNames = new string[] { "字段名称" }; 取值 string aaa= GridView. ...
 - php   编程效率(1)
			
用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则 不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数”(译注:PHP手册中 ...
 - ueditor   文本编辑器
			
百度编辑器 压缩包在文件里 百度UEditor编辑器使用教程与使用方法 发布时间:2014-08-23 14:23:34.0 作者:青岛做网站 我们在做网站的时候,网站后台系统一般都会用到 ...