博客
关于我
强烈建议你试试无所不能的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

你可能感兴趣的文章
backgound-attachment属性学习
查看>>
个人作业——关于K米的产品案例分析
查看>>
基于node/mongo的App Docker化测试环境搭建
查看>>
java web 中base64传输的坑
查看>>
java 中的线程(一)
查看>>
秒杀9种排序算法(JavaScript版)
查看>>
素数判断BFS之“Prime Path”
查看>>
Activiti入门 -- 环境搭建和核心API简介
查看>>
struts.convention.classes.reload配置为true,tomcat启动报错
查看>>
IOS push消息的数字不减少的问题
查看>>
mysql报错Multi-statement transaction required more than 'max_binlog_cache_size' bytes of storage
查看>>
MySQL的并行复制多线程复制MTS(Multi-Threaded Slaves)
查看>>
Django中间件
查看>>
Hdfs数据备份
查看>>
xcode 5.1安装vvdocument
查看>>
log4j:WARN No appenders could be found for logger
查看>>
Google翻译
查看>>
盖得化工--采集所有公司详细信息
查看>>
Logistic Ordinal Regression
查看>>
常用软件
查看>>