2013-01-24 2 views
0

JavaScript에서 대소 문자를 구분하는 문자열 대체를 수행하려면 어떻게해야합니까? 나는 PHP의 str_replace과 비슷한 것을 찾고있다. JavaScript에서 PHP str_replace와 동등한 작업을 수행하려면 어떻게해야합니까?

var text = "this IS some sample text that is going to be replaced"; 
var new_text = replace_in_javascript("IS", "is not", text); 
document.write(new_text); 

this is not some sample text that is going to be replaced 

내가 모든 발행 수를 교체하고 싶습니다 제공해야합니다.

var str = 'is not'; 
str = str.replace(/IS/g, 'something'); // won't replace "is" 

g 글로벌입니다 :

+1

정규식 당신이 원하는 것입니다. 'replace()'를보고'i' 플래그를 생략하십시오. 또한 전역 일치에'g' 플래그를 포함하십시오. – jeremy

+0

[이 게시물은 문제를 해결합니다] (http://stackoverflow.com/questions/280793/case-insensitive-string-replacement-in-javascript)처럼 보입니다. – JakeGould

+1

'str_ireplace'는 대소 문자를 구별하지 않으므로, PHP에서 거꾸로 붙인 것처럼 보입니다 – bfavaretto

답변

4

당신은 정규 표현식을 사용할 수 있습니다.

대/소문자를 구분하지 않으려면 i 플래그 /is/gi을 추가하십시오. PHP 함수와 정확히 같은

+0

정말 답이 되려면'g' 플래그 사용법을 설명해야합니다. PHP에서 오는 누군가는 분명히 글로벌 대체품을 기대할 것입니다. 포스터가 두 가지 방법으로 사용할 수 있도록 대소 문자를 구분하지 않는 시나리오의 'i' 플래그도 설명해야합니다. –

+0

> 모든 현상을 대체하고 싶습니다. – jeremy

+0

@bfavaretto 이것은 대소 문자 구별입니다 ** ** ** OP 읽기 – jeremy

0

작품 :

function str_ireplace(search, replace, subject) { 
    // discuss at: http://phpjs.org/functions/str_ireplace/ 
    // original by: Martijn Wieringa 
    // input by: penutbutterjelly 
    // input by: Brett Zamir (http://brett-zamir.me) 
    // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 
    // improved by: Jack 
    // bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 
    // bugfixed by: Onno Marsman 
    // bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) 
    // bugfixed by: Philipp Lenssen 
    // example 1: str_ireplace('l', 'l', 'HeLLo'); 
    // returns 1: 'Hello' 
    // example 2: str_ireplace('$', 'foo', '$bar'); 
    // returns 2: 'foobar' 

    var i, k = ''; 
    var searchl = 0; 
    var reg; 

    var escapeRegex = function(s) { 
    return s.replace(/([\\\^\$*+\[\]?{}.=!:(|)])/g, '\\$1'); 
    }; 

    search += ''; 
    searchl = search.length; 
    if (Object.prototype.toString.call(replace) !== '[object Array]') { 
    replace = [replace]; 
    if (Object.prototype.toString.call(search) === '[object Array]') { 
     // If search is an array and replace is a string, 
     // then this replacement string is used for every value of search 
     while (searchl > replace.length) { 
     replace[replace.length] = replace[0]; 
     } 
    } 
    } 

    if (Object.prototype.toString.call(search) !== '[object Array]') { 
    search = [search]; 
    } 
    while (search.length > replace.length) { 
    // If replace has fewer values than search, 
    // then an empty string is used for the rest of replacement values 
    replace[replace.length] = ''; 
    } 

    if (Object.prototype.toString.call(subject) === '[object Array]') { 
    // If subject is an array, then the search and replace is performed 
    // with every entry of subject , and the return value is an array as well. 
    for (k in subject) { 
     if (subject.hasOwnProperty(k)) { 
     subject[k] = str_ireplace(search, replace, subject[k]); 
     } 
    } 
    return subject; 
    } 

    searchl = search.length; 
    for (i = 0; i < searchl; i++) { 
    reg = new RegExp(escapeRegex(search[i]), 'gi'); 
    subject = subject.replace(reg, replace[i]); 
    } 

    return subject; 
} 

원본 : http://phpjs.org/functions/str_ireplace/

관련 문제