博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Javascript] How to use JavaScript's String.replace
阅读量:5094 次
发布时间:2019-06-13

本文共 4749 字,大约阅读时间需要 15 分钟。

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 interesting 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 Google, tweet with Twitter'    var result = input.replace(/\[(.*?)\]\((.*?)\)/g, '$1')  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 Google, tweet with Twitter'    var result = input.replace(/\[(.*?)\]\((.*?)\)/g, '$1')  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(', ')  }}

 

转载于:https://www.cnblogs.com/Answer1215/p/5132177.html

你可能感兴趣的文章
使用vue脚手架搭建项目
查看>>
Java基础之ArrayList与LinkedList、Vector,以及HashMap与HashTable的区别
查看>>
网络爬虫初步:从一个入口链接开始不断抓取页面中的网址并入库
查看>>
iOS archive(归档)的总结 (序列化和反序列化,持久化到文件)
查看>>
python第九天课程:遇到了金角大王
查看>>
字符串处理
查看>>
ECharts(Enterprise Charts 商业产品图表库)初识
查看>>
LeetCode Factorial Trailing Zeroes (阶乘后缀零)
查看>>
hdu 5402 Travelling Salesman Problem (技巧,未写完)
查看>>
[AIR] 获取U盘,打开U盘
查看>>
HtmlUnitDriver 网页内容动态抓取
查看>>
django url 路由设置技巧
查看>>
三言两语说清“线性流程”
查看>>
(转)虚函数和纯虚函数区别
查看>>
ad logon hour
查看>>
获得进程可执行文件的路径: GetModuleFileNameEx, GetProcessImageFileName, QueryFullProcessImageName...
查看>>
证件照(1寸2寸)拍摄处理知识汇总
查看>>
Git入门简介
查看>>
eclipse里maven install时,报错提示jdk为无效的目标版本:1.7
查看>>
罗马数字与阿拉伯数字转换
查看>>