web-dev-qa-db-fra.com

Remplacer plusieurs chaînes par plusieurs autres chaînes

J'essaie de remplacer plusieurs mots d'une chaîne par plusieurs autres mots. La chaîne est "J'ai un chat, un chien et une chèvre."

Cependant, cela ne produit pas "j'ai un chien, une chèvre et un chat", mais plutôt "j'ai un chat, un chat et un chat". Est-il possible de remplacer plusieurs chaînes par plusieurs autres chaînes en même temps en JavaScript, afin d'obtenir le résultat correct?

var str = "I have a cat, a dog, and a goat.";
str = str.replace(/cat/gi, "dog");
str = str.replace(/dog/gi, "goat");
str = str.replace(/goat/gi, "cat");

//this produces "I have a cat, a cat, and a cat"
//but I wanted to produce the string "I have a dog, a goat, and a cat".
149
Anderson Green

Solution spécifique

Vous pouvez utiliser une fonction pour les remplacer.

var str = "I have a cat, a dog, and a goat.";
var mapObj = {
   cat:"dog",
   dog:"goat",
   goat:"cat"
};
str = str.replace(/cat|dog|goat/gi, function(matched){
  return mapObj[matched];
});

exemple jsfiddle

Généraliser

Si vous souhaitez conserver dynamiquement la regex et simplement ajouter des échanges futurs à la carte, vous pouvez le faire.

new RegExp(Object.keys(mapObj).join("|"),"gi"); 

générer la regex. Alors, cela ressemblerait à ceci

var mapObj = {cat:"dog",dog:"goat",goat:"cat"};

var re = new RegExp(Object.keys(mapObj).join("|"),"gi");
str = str.replace(re, function(matched){
  return mapObj[matched];
});

Et pour ajouter ou modifier d'autres remplacements, vous pouvez simplement modifier la carte.

violon avec regex dynamique

Le rendant réutilisable

Si vous voulez que cela soit un motif général, vous pouvez le tirer vers une fonction comme celle-ci.

function replaceAll(str,mapObj){
    var re = new RegExp(Object.keys(mapObj).join("|"),"gi");

    return str.replace(re, function(matched){
        return mapObj[matched.toLowerCase()];
    });
}

Vous pouvez alors simplement passer la chaîne et une carte des remplacements que vous voulez à la fonction et la chaîne transformée sera renvoyée.

violon avec fonction

Pour vous assurer que Object.keys fonctionne dans les navigateurs plus anciens, ajoutez un polyfill, par exemple, à partir de MDN ou Es5 .

331
Ben McCormick

Cela ne répond peut-être pas exactement à vos besoins dans ce cas, mais je trouve que c'est un moyen utile de remplacer plusieurs paramètres dans des chaînes, en tant que solution générale. Il remplacera toutes les occurrences des paramètres, peu importe le nombre de fois où ils sont référencés:

String.prototype.fmt = function (hash) {
        var string = this, key; for (key in hash) string = string.replace(new RegExp('\\{' + key + '\\}', 'gm'), hash[key]); return string
}

Vous l'invoqueriez comme suit:

var person = '{title} {first} {last}'.fmt({ title: 'Agent', first: 'Jack', last: 'Bauer' });
// person = 'Agent Jack Bauer'
6
Iian

Cela a fonctionné pour moi:

String.prototype.replaceAll = function(search, replacement) {
    var target = this;
    return target.replace(new RegExp(search, 'g'), replacement);
};

function replaceAll(str, map){
    for(key in map){
        str = str.replaceAll(key, map[key]);
    }
    return str;
}

//testing...
var str = "bat, ball, cat";
var map = {
    'bat' : 'foo',
    'ball' : 'boo',
    'cat' : 'bar'
};
var new = replaceAll(str, map);
//result: "foo, boo, bar"
3
João Paulo

fonction utilisateur normale pour définir le modèle à remplacer, puis utiliser la fonction de remplacement pour travailler sur la chaîne d'entrée,

var i = new RegExp('"{','g'),
    j = new RegExp('}"','g'),
    k = data.replace(i,'{').replace(j,'}');
2
KARTHIKEYAN.A

Utilisez des éléments numérotés pour éviter de les remplacer à nouveau, par exemple

let str = "I have a %1, a %2, and a %3";
let pets = ["dog","cat", "goat"];

puis

str.replace(/%(\d+)/g, (_, n) => pets[+n-1])

Comment ça marche: -%\D + trouve les nombres qui suivent un%. Les crochets capturent le nombre.

Ce nombre (sous forme de chaîne) est le deuxième paramètre, n, de la fonction lambda.

Le + n-1 convertit la chaîne en nombre puis soustrait 1 pour indexer le tableau pets.

Le% number est alors remplacé par la chaîne à l'index du tableau.

Avec/g, la fonction lambda est appelée de manière répétée avec chaque numéro qui est ensuite remplacé par une chaîne du tableau.

En JavaScript moderne: -

replace_n=(str,...ns)=>str.replace(/%(\d+)/g,(_,n)=>ns[n-1])
1
Quentin 2

Juste au cas où quelqu'un se demanderait pourquoi la solution de l'affiche originale ne fonctionne pas:

var str = "I have a cat, a dog, and a goat.";

str = str.replace(/cat/gi, "dog");
// now str = "I have a dog, a dog, and a goat."

str = str.replace(/dog/gi, "goat");
// now str = "I have a goat, a goat, and a goat."

str = str.replace(/goat/gi, "cat");
// now str = "I have a cat, a cat, and a cat."
1
jack

en utilisant Array.prototype.reduce () :

const arrayOfObjects = [
  { plants: 'men' },
  { smart:'dumb' },
  { peace: 'war' }
]
const sentence = 'plants are smart'

arrayOfObjects.reduce(
  (f, s) => `${f}`.replace(Object.keys(s)[0], s[Object.keys(s)[0]]), sentence
)

// as a reusable function
const replaceManyStr = (obj, sentence) => obj.reduce((f, s) => `${f}`.replace(Object.keys(s)[0], s[Object.keys(s)[0]]), sentence)

const result = replaceManyStr(arrayOfObjects , sentence1)

Exemple

// /////////////    1. replacing using reduce and objects

// arrayOfObjects.reduce((f, s) => `${f}`.replace(Object.keys(s)[0], s[Object.keys(s)[0]]), sentence)

// replaces the key in object with its value if found in the sentence
// doesn't break if words aren't found

// Example

const arrayOfObjects = [
  { plants: 'men' },
  { smart:'dumb' },
  { peace: 'war' }
]
const sentence1 = 'plants are smart'
const result1 = arrayOfObjects.reduce((f, s) => `${f}`.replace(Object.keys(s)[0], s[Object.keys(s)[0]]), sentence1)

console.log(result1)

// result1: 
// men are dumb


// Extra: string insertion python style with an array of words and indexes

// usage

// arrayOfWords.reduce((f, s, i) => `${f}`.replace(`{${i}}`, s), sentence)

// where arrayOfWords has words you want to insert in sentence

// Example

// replaces as many words in the sentence as are defined in the arrayOfWords
// use python type {0}, {1} etc notation

// five to replace
const sentence2 = '{0} is {1} and {2} are {3} every {5}'

// but four in array? doesn't break
const words2 = ['man','dumb','plants','smart']

// what happens ?
const result2 = words2.reduce((f, s, i) => `${f}`.replace(`{${i}}`, s), sentence2)

console.log(result2)

// result2: 
// man is dumb and plants are smart every {5}

// replaces as many words as are defined in the array
// three to replace
const sentence3 = '{0} is {1} and {2}'

// but five in array
const words3 = ['man','dumb','plant','smart']

// what happens ? doesn't break
const result3 = words3.reduce((f, s, i) => `${f}`.replace(`{${i}}`, s), sentence3)

console.log(result3)

// result3: 
// man is dumb and plants
1
Emmanuel N K
    var str = "I have a cat, a dog, and a goat.";

    str = str.replace(/goat/i, "cat");
    // now str = "I have a cat, a dog, and a cat."

    str = str.replace(/dog/i, "goat");
    // now str = "I have a cat, a goat, and a cat."

    str = str.replace(/cat/i, "dog");
    // now str = "I have a dog, a goat, and a cat."
0
Anand Kumar Singh
String.prototype.replaceSome = function() {
    var replaceWith = Array.prototype.pop.apply(arguments),
        i = 0,
        r = this,
        l = arguments.length;
    for (;i<l;i++) {
        r = r.replace(arguments[i],replaceWith);
    }
    return r;
}

/ * replaceSome une méthode pour stringsit prend comme autant d'arguments que nous voulons et en remplace tous les par le dernier argument que nous avons spécifié 2013 CopyRights sauvegardé pour: Max Ahmed c'est-à-dire un exemple:

var string = "[hello i want to 'replace x' with eat]";
var replaced = string.replaceSome("]","[","'replace x' with","");
document.write(string + "<br>" + replaced); // returns hello i want to eat (without brackets)

* /

jsFiddle: http://jsfiddle.net/CPj89/

0
nazih ahmed

Utilisation de jQuery Remplacez plusieurs chaînes par plusieurs autres chaînes:

var replacetext = {
    "abc": "123",
    "def": "456"
    "ghi": "789"
};

$.each(replacetext, function(txtorig, txtnew) {
    $(".eng-to-urd").each(function() {
        $(this).text($(this).text().replace(txtorig, txtnew));
    });
});
0
Super Model
<!DOCTYPE html>
<html>
<body>



<p id="demo">Mr Blue 
has a           blue house and a blue car.</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
    var str = document.getElementById("demo").innerHTML;
    var res = str.replace(/\n| |car/gi, function myFunction(x){

if(x=='\n'){return x='<br>';}
if(x==' '){return x='&nbsp';}
if(x=='car'){return x='BMW'}
else{return x;}//must need



});

    document.getElementById("demo").innerHTML = res;
}
</script>

</body>
</html>
0
Jalaluddin Rumi

Avec mon replace-once package, vous pouvez effectuer les opérations suivantes:

const replaceOnce = require('replace-once')

var str = 'I have a cat, a dog, and a goat.'
var find = ['cat', 'dog', 'goat']
var replace = ['dog', 'goat', 'cat']
replaceOnce(str, find, replace, 'gi')
//=> 'I have a dog, a goat, and a cat.'
0
Kodie Grantham

J'ai écrit ce paquet npm: stringinject https://www.npmjs.com/package/stringinject qui vous permet d'effectuer les opérations suivantes

var string = stringInject("this is a {0} string for {1}", ["test", "stringInject"]);

qui remplacera {0} et {1} par les éléments du tableau et retournera la chaîne suivante

"this is a test string for stringInject"

ou vous pouvez remplacer les espaces réservés par des clés d'objet et des valeurs comme ceci:

var str = stringInject("My username is {username} on {platform}", { username: "tjcafferkey", platform: "GitHub" });

"My username is tjcafferkey on Github" 
0
tjcafferkey