Programing

여러 개의 문자열을 다른 여러 개의 문자열로 교체

lottogame 2020. 5. 20. 07:49
반응형

여러 개의 문자열을 다른 여러 개의 문자열로 교체


문자열의 여러 단어를 다른 여러 단어로 바꾸려고합니다. 줄은 "고양이, 개, 염소입니다."

그러나 이것은 "개, 염소 및 고양이가 있습니다"를 생산하지 않지만 대신 "고양이, 고양이 및 고양이가 있습니다"를 생산합니다. JavaScript에서 여러 문자열을 여러 개의 다른 문자열로 동시에 교체하여 올바른 결과를 얻을 수 있습니까?

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".

특정 솔루션

함수를 사용하여 각각을 교체 할 수 있습니다.

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];
});

jsfiddle 예제

그것을 일반화

정규식을 동적으로 유지하고 향후 교환을 맵에 추가하려면 다음을 수행하십시오.

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

정규식을 생성합니다. 그러면 다음과 같이 보일 것입니다

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];
});

대체물을 더 추가하거나 변경하려면 맵을 편집하면됩니다. 

역동적 인 정규 표현식으로 바이올린

재사용 가능하게 만들기

이것을 일반적인 패턴으로 만들고 싶다면 이것을 다음과 같은 함수로 끌어낼 수 있습니다.

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

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

그런 다음 str과 원하는 대체물의 맵을 함수에 전달하면 변환 된 문자열이 반환됩니다.

기능을 가진 바이올린

이전 브라우저에서 Object.keys가 작동하도록하려면 MDN 또는 Es5 등 의 폴리 필을 추가하십시오 .


이것은이 인스턴스의 정확한 요구를 충족시키지 못할 수도 있지만 일반적인 해결책으로 문자열의 여러 매개 변수를 대체하는 유용한 방법을 찾았습니다. 참조 횟수에 관계없이 모든 매개 변수 인스턴스를 대체합니다.

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

다음과 같이 호출하십시오.

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

다시 교체하지 않도록 번호가 매겨진 품목을 사용하십시오. 예 :

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

그때

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

작동 방식 :-% \ d +는 % 뒤에 오는 숫자를 찾습니다. 대괄호는 숫자를 캡처합니다.

이 숫자 (문자열)는 람다 함수의 두 번째 매개 변수 n입니다.

+ n-1은 문자열을 숫자로 변환 한 다음 pets 배열을 인덱싱하기 위해 1을 뺍니다.

그런 다음 % number는 배열 인덱스에서 문자열로 바뀝니다.

/ g는 람다 함수가 각 숫자로 반복적으로 호출 된 다음 배열의 문자열로 대체됩니다.

현대 JavaScript에서 :-

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

이것은 나를 위해 일했다 :

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"

바꿀 패턴을 정의한 다음 replace 함수를 사용하여 입력 문자열을 처리하는 사용자 일반 함수,

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

사용 ) (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)

// /////////////    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


누군가 원래 포스터의 솔루션이 작동하지 않는 이유를 궁금해하는 경우를 대비하여 :

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."

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;
}

/ * replace 문자열에 대해 몇 가지 메소드를 사용합니다. 원하는만큼 인수를 사용하고 모든 문자열을 2013 년에 지정한 마지막 인수로 바꿉니다. Max Ahmed 다음은 예입니다.

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/


<!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>

I wrote this npm package stringinject https://www.npmjs.com/package/stringinject which allows you to do the following

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

which will replace the {0} and {1} with the array items and return the following string

"this is a test string for stringInject"

or you could replace placeholders with object keys and values like so:

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

"My username is tjcafferkey on Github" 

With my replace-once package, you could do the following:

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.'

    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."

I expanded on @BenMcCormicks a bit. His worked for regular strings but not if I had escaped characters or wildcards. Here's what I did

str = "[curl] 6: blah blah 234433 blah blah";
mapObj = {'\\[curl] *': '', '\\d: *': ''};


function replaceAll (str, mapObj) {

    var arr = Object.keys(mapObj),
        re;

    $.each(arr, function (key, value) {
        re = new RegExp(value, "g");
        str = str.replace(re, function (matched) {
            return mapObj[value];
        });
    });

    return str;

}
replaceAll(str, mapObj)

returns "blah blah 234433 blah blah"

This way it will match the key in the mapObj and not the matched word'


Simple solution with Jquery: Replace multiple strings with multiple other strings:

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));
    });
});

참고URL : https://stackoverflow.com/questions/15604140/replace-multiple-strings-with-multiple-other-strings

반응형