[Javascript] How to use JavaScript's String.replace
In JavaScript, you can change the content of a string using the replace method. This method signature is overloaded with a bunch of different ways to do string replacement in JavaScript. This lesson covers the entire API (including an interestingDSL for the replacement string).
console.clear() simpleStringReplace()
simpleRegexReplace()
globalRegexReplace()
regexReplaceGroup()
advancedRegexReplaceGroup()
simpleFunctionReplacer()
regexFunctionReplacerGroup() console.log('Tests passed') function simpleStringReplace() {
var input = 'Perl is the best programming language'
var expected = 'JavaScript is the best programming language' var result = input.replace('Perl', 'JavaScript')
expect(result).toEqual(expected)
} function simpleRegexReplace() {
var input = 'My phone number is 123-555-3344'
var expected = 'My phone number is xxx-xxx-xxxx' var result = input.replace(/\d{3}-\d{3}-\d{4}/, 'xxx-xxx-xxxx')
expect(result).toEqual(expected)
} function globalRegexReplace() {
var input = 'The basketball is round, and the basketball is bouncy'
var expected = 'The soccerball is round, and the soccerball is bouncy' var result = input.replace(/basketball/ig, 'soccerball')
expect(result).toEqual(expected)
} function regexReplaceGroup() {
var input = 'Search with [Google](https://google.com), tweet with [Twitter](https://twitter.com)'
var expected = 'Search with <a href="https://google.com">Google</a>, tweet with <a href="https://twitter.com">Twitter</a>' var result = input.replace(/\[(.*?)\]\((.*?)\)/g, '<a href="$2">$1</a>')
expect(result).toEqual(expected)
} function advancedRegexReplaceGroup() {
var input = 'foobarbaz'
var expected = 'foofoo-bar-$-bazbaz' var result = input.replace('bar', '$`-$&-$$-$\'')
expect(result).toEqual(expected)
} function simpleFunctionReplacer() {
var input = 'theAnswerToLifeTheUniverseAndEverything'
var expected = 'the-answer-to-life-the-universe-and-everything' var result = input.replace(/[A-Z]/g, upperToHyphenLower)
expect(result).toEqual(expected) function upperToHyphenLower(match) {
return '-' + match.toLowerCase();
}
} function regexFunctionReplacerGroup() {
var input = 'These are the replacer arguments'
var expected = 'These are the replacer arguments: replacer, arguments, 14, These are the replacer arguments' var result = input.replace(/(replacer) (arguments)/, replacer) expect(result).toEqual(expected) function replacer(match, group1, group2, offset, wholeString) {
return match + ': ' + [group1, group2, offset, wholeString].join(', ')
}
} console.clear() simpleStringReplace()
simpleRegexReplace()
globalRegexReplace()
regexReplaceGroup()
advancedRegexReplaceGroup()
simpleFunctionReplacer()
regexFunctionReplacerGroup() console.log('Tests passed') function simpleStringReplace() {
var input = 'Perl is the best programming language'
var expected = 'JavaScript is the best programming language' var result = input.replace('Perl', 'JavaScript')
expect(result).toEqual(expected)
} function simpleRegexReplace() {
var input = 'My phone number is 123-555-3344'
var expected = 'My phone number is xxx-xxx-xxxx' var result = input.replace(/\d{3}-\d{3}-\d{4}/, 'xxx-xxx-xxxx')
expect(result).toEqual(expected)
} function globalRegexReplace() {
var input = 'The basketball is round, and the basketball is bouncy'
var expected = 'The soccerball is round, and the soccerball is bouncy' var result = input.replace(/basketball/ig, 'soccerball')
expect(result).toEqual(expected)
} function regexReplaceGroup() {
var input = 'Search with [Google](https://google.com), tweet with [Twitter](https://twitter.com)'
var expected = 'Search with <a href="https://google.com">Google</a>, tweet with <a href="https://twitter.com">Twitter</a>' var result = input.replace(/\[(.*?)\]\((.*?)\)/g, '<a href="$2">$1</a>')
expect(result).toEqual(expected); // $2 --> (.*?) --> (https://google.com)
// $1 --> [.*?] --> [Google]
} function advancedRegexReplaceGroup() {
var input = 'foobarbaz'
var expected = 'foofoo-bar-$-bazbaz' var result = input.replace('bar', '$`-$&-$$-$\'')
expect(result).toEqual(expected)
} function simpleFunctionReplacer() {
var input = 'theAnswerToLifeTheUniverseAndEverything'
var expected = 'the-answer-to-life-the-universe-and-everything' var result = input.replace(/[A-Z]/g, upperToHyphenLower)
expect(result).toEqual(expected) function upperToHyphenLower(match) {
return '-' + match.toLowerCase();
}
} function regexFunctionReplacerGroup() {
var input = 'These are the replacer arguments'
var expected = 'These are the replacer arguments: replacer, arguments, 14, These are the replacer arguments' var result = input.replace(/(replacer) (arguments)/, replacer) expect(result).toEqual(expected) function replacer(match, group1, group2, offset, wholeString) {
return match + ': ' + [group1, group2, offset, wholeString].join(', ')
}
}
[Javascript] How to use JavaScript's String.replace的更多相关文章
- <JavaScript语言精粹>--<读书笔记三>之replace()与正则
今天有人问我repalce(),他那个题目很有意思.我也不会做,于是我就去查,结果发现就是最基础的知识的延伸. 所以啊最基础的知识才是很重要的,千万不能忽略,抓起JS就写代码完全不知到所以然,只知道写 ...
- javascript基础知识梳理-Number与String之间的互相转换【转】
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- how to convert a number to a number array in javascript without convert number to a string
how to convert a number to a number array in javascript without convert number to a string 如何在不将数字转换 ...
- javascript内置对象一:String
<script> //string.split("-"),以字符"-"把string拆分成一个数组.返回一个数组 //array.joi ...
- [转]String.Replace 和 String.ReplaceAll 的区别
JAVA 中的 replace replaceAll 问题: 测试code System.out.println("1234567890abcdef -----> "+&qu ...
- JAVA中string.replace()和string.replaceAll()的区别及用法
乍一看,字面上理解好像replace只替换第一个出现的字符(受javascript的影响),replaceall替换所有的字符,其实大不然,只是替换的用途不一样. public String r ...
- 如何选择Javascript模板引擎(javascript template engine)?
译者 jjfat 日期:2012-9-17 来源: GBin1.com 随着前端开发的密集度越来越高,Ajax和JSON的使用越来越频繁,大家肯定免不了在前台开发中大量的使用标签,常见到的例子如下: ...
- JavaScript大杂烩1 - 理解JavaScript的类型系统
随着硬件水平的逐渐提高,浏览器的处理能力越来越强大,本人坚信,客户端会越来越瘦,瘦到只用浏览器就够了,服务端会越来越丰满:虽然很多大型的程序,比如3D软件,客户端仍然会存在,但是未来的主流必将是浏览器 ...
- python 全栈开发,Day50(Javascript简介,第一个JavaScript代码,数据类型,运算符,数据类型转换,流程控制,百度换肤,显示隐藏)
一.Javascript简介 Web前端有三层: HTML:从语义的角度,描述页面结构 CSS:从审美的角度,描述样式(美化页面) JavaScript:从交互的角度,描述行为(提升用户体验) Jav ...
- 一、JavaScript概述 二、JavaScript的语法 三、JavaScript的内置对象
一.JavaScript的概述###<1>JavaScript的概念 又称ECMAScript,和java没有任何关系 嵌入在HTML元素中的 被浏览器解释运行的 一种脚本语言. ###& ...
随机推荐
- FoxOne---一个快速高效的BS框架--(4)
FoxOne---一个快速高效的BS框架--(1) FoxOne---一个快速高效的BS框架--(2) FoxOne---一个快速高效的BS框架--(3) FoxOne---一个快速高效的BS框架-- ...
- C#自定义List类
代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespac ...
- 在Linux系统中如何把文件拷贝到U盘
Linux下把所有的都当成文件处理,如果在linux系统下需要拷贝文件,哪么你需要先把U盘挂载到系统中的某一个位置,然后再使用cp命令完成拷贝. 工具/原料 Linux操作系统一台 U盘一枚 方法 ...
- Python学习--07迭代器、生成器
迭代 如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们称为迭代(Iteration). Python里使用for...in来迭代. 常用可迭代对象有 ...
- JAVA-FileInputStream之read方法
今天一个友询问FileInputStrem方法的read()和read(byte b) 方法为什么都用-1来判断读文件结束的问题,在此和大家一起学习下. 关于FileInputStream 它用于读取 ...
- Android -------- RelativeLayout 和 LinearLayout 的性能分析
布局的绘制角度 RelativeLayout不如LinearLayout快的根本原因是: RelativeLayout需要对其子View进行两次measure过程, 而LinearLayout则只需一 ...
- 常用webservice网址
http://www.gpsso.com/Main/ServiceList.aspx http://developer.51cto.com/art/200908/147125.htm 这里记录了几个常 ...
- 在iOS虚拟机上使CLPlacemark获取中文信息
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ CLLocat ...
- Java protobuf框架使用向导
ProtoBuf,全称是Protocol Buffers, 它是谷歌内部用的一种高效的.可扩展的对结构化数据进行编码的格式规范.谷歌自己内部很多程序之间的通信协议都用了ProtoBuf. 下面介绍的是 ...
- 一个Hadoop难以查找的错误
This script is Deprecated. Instead use start-dfs.sh and start-yarn.sh Starting namenodes on [Master1 ...