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 ...
随机推荐
- ios中的关键词@property @synthesize
@interface Person : NSObject{ int myNumber;} @property(nonatomic) int myNumber;//这个关键字是可以带套get 与s ...
- 【读书笔记】C Primer Plus ch.15位运算 示例程序15.1 整数转换成二进制字符串
正文: https://www.zybuluo.com/RayChen/note/595213
- listview条目用状态选择器没反应
button和imagebutton天生具有“可点击(click)”.“可按下(press)”的特性,radiobutton具有“可勾选(check)”的特性,但是listview的条目只有“可按下( ...
- vs 发布web应用程序时,找不到cs文件错误
将*.aspx.*.ascx.*.master所有出错页面文件中的 CodeFile="******.aspx.cs" 批量替换成 Codebehind="******. ...
- spice-vdagent
The spice-vdagent should be running in the guest. Have you installed the spice guest tools in your w ...
- php UNIX时间戳转换为指定日期格式
用函数: date() 一般形式:date('Y-m-d H:i:s', unix时间) $date_unix=time();//获取当前时间,unix时间戳 echo 'Unix时间:'.$date ...
- JSTL判断list的size()大小
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ tag ...
- javaweb 国际化
国际化又称为 i18n:internationalization 软件实现国际化,需具备哪些特征:对于程序中固定使用的文本元素,例如菜单栏.导航条等中使用的文本元素.或错误提示信息,状态信息等,需要根 ...
- python解析XML之ElementTree
#coding=utf-8 from xml.etree import ElementTree as ET tree=ET.parse('test.xml') root = tree.getroot( ...
- 8--UI 初步认识 简易计算器
UI是App的根基:一个App应该是先有UI界面,然后在UI的基础上增加实用功能(2)UI相对简单易学:UI普遍是学习过程中最简单的一块,能快速拥有成就感和学习兴趣(3)UI至关重要:开发中的绝大部分 ...