简体   繁体   English

JavaScript - 从优化的角度来看,应该如何用不同的字符串替换字符串中的多个子字符串?

[英]JavaScript - From an optimization standpoint how should one replace multiple substrings in a string with different strings?

In the code that I develop and maintain I have ran into an issue.在我开发和维护的代码中,我遇到了一个问题。

I have a function that takes a query (type string) and replaces substrings of that string with a different string.我有一个函数,它接受一个查询(字符串类型)并用不同的字符串替换该字符串的子字符串。 As an example if a user types in the string I have a cat it would replace it with I have a dog .例如,如果用户输入字符串I have a cat它会将其替换为I have a dog

My code works but the problem is I have hundreds of such substrings that need to be replaced with a different one.我的代码有效,但问题是我有数百个这样的子字符串,需要用不同的子字符串替换。 It also looks really bad aesthetically.它在美学上看起来也很糟糕。

var myString;
myString = myString.replace('cat','dog')
                   .replace('elephant','zebra')
                   .replace('bird','fish')
                   // Goes on for hundreds of lines

This is all inside a function in which everytime it's called it goes through hundreds of replace calls.这一切都在一个函数中,每次调用它都会经历数百次replace调用。

One thing I could try doing is creating an array of objects and iterating through it.我可以尝试做的一件事是创建一个对象数组并遍历它。 The code would look something like this.代码看起来像这样。

var animalsArray = [
                       {'a':'cat','b':'dog'},
                       {'a':'elephant','b':'zebra'},
                       {'a':'bird','b':'fish'}
                   ];

And then in my function然后在我的函数中

function stringReplace(string) {
    for (var i = 0; i < animalsArray.length; i++) {
        if (string.indexOf(animalsArray[i]['a']) > -1) {
            sting = string.replace(animalsArray[i]['a'],animalsArray[i]['b']);
        }
    }
}

But I'm not sure whether that would improve on my current practice of chaining together hundreds of replace calls.但我不确定这是否会改善我目前将数百个替换调用链接在一起的做法。

I'm basically looking to optimize my current code.我基本上是想优化我当前的代码。 What is the best practice?最佳做法是什么?

You can make a regaulr expression with a bunch of or statements.你可以用一堆 or 语句来制作一个regaulr表达式。 (dog|elephant|bird|....) and that will allow you to run one check. (dog|elephant|bird|....)这将允许您运行一张支票。 The replace gives you the matched text which you can use to look up the word to replace.替换为您提供匹配的文本,您可以使用它来查找要替换的单词。

So make an object of the strings to replace and their value is the word to replace.因此,创建一个要替换的字符串对象,它们的值就是要替换的单词。 You can just look up the replacement by the matched key.您可以通过匹配的键查找替换。

 const animals = { cat: 'dog', elephant: 'zebra', bird: 'fish', } // build the or sting with a simple join const keysString = Object.keys(animals).join("|") // generate the regular expression to match the words var animalRE = new RegExp(`\\\\b(${keysString})\\\\b`,'g'); // a sample string to match const myString = "I like to pet a cat that looks like an elephant that moves like a bird." // the replacement step. The function takes the matched text (aka key) and looks it up in the object const updated = myString.replace(animalRE, key => animals[key] || key) // display the new string console.log(updated)

I'd consider using an object instead, whose keys are the substrings to be replaced, and whose values are the replacements.我会考虑使用一个对象,其键是要替换的子字符串,其值是替换。 Make a regular expression by alternating all of the object's keys, then use a replacer function to look up the associated value on the object:通过交替对象的所有键来创建正则表达式,然后使用替换函数查找对象上的关联值:

 const replacements = { cat: 'dog', elephant: 'zebra', bird: 'fish' }; const pattern = new RegExp(Object.keys(replacements).join('|'), 'g'); console.log('Foo cat bar bird'.replace(pattern, match => replacements[match]));

Using object syntax makes it easy to add/remove items.使用对象语法可以轻松添加/删除项目。 If you want to make it even easier to modify, you could consider putting the replacement information into a string instead, then parsing it into an object:如果你想让它更容易修改,你可以考虑将替换信息放入一个字符串中,然后将其解析为一个对象:

 const replacementsStr = ` cat dog elephant zebra bird fish `; const replacements = Object.fromEntries( replacementsStr .trim() .split('\\n') .map(line => line.split(/\\s+/)) ); const pattern = new RegExp(Object.keys(replacements).join('|'), 'g'); console.log('Foo cat bar bird'.replace(pattern, match => replacements[match]));

I would do something like:我会做这样的事情:

 function WordSwapper(sentence){ const swapper = sentence; this.swaps = []; this.add = (word, replacer)=>{ this.swaps.push([word, replacer]); return this; } this.swap = (sentence = null)=>{ let s = sentence === null ? swapper : sentence; this.swaps.forEach(a=>{ s = s.replace(new RegExp(a[0], 'gi'), a[1]); }); return s; } } const ws = new WordSwapper('The cat plays. Elephants live in the Serengeti. Have you ever seen a bird fly?'); ws.add('cat', 'dog').add('elephant', 'zebra').add('bird', 'fish'); console.log(ws.swap());

Of course, you may want to do something with plural and uppercase situations.当然,您可能想要对复数和大写情况做一些事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM