logo

JavaScript 문자열 바꾸기() 메서드

자바스크립트에서는 교체() 메서드 문자열을 조작하는 데 사용됩니다. 이를 통해 문자열의 특정 부분을 검색할 수 있습니다. 하위 문자열 을 선택한 다음 다른 것으로 교체하세요. 하위 문자열 . 좋은 점은 이 방법이 원래 문자열을 변경하지 않습니다 , 원본 데이터의 무결성을 유지하려는 작업에 이상적입니다. 사용방법 이해하기 바꾸다() JavaScript에서 문자열을 효과적으로 작업하는 능력을 향상시킬 수 있습니다.

통사론:



str.replace(value1, value2);>
  • 값1: 대체할 정규식입니다.
  • 값2: 주어진 문자열의 내용을 대체할 문자열입니다.

반환 값:

대체된 항목이 포함된 새 문자열을 반환합니다.

문자열 바꾸기() 메서드 예:

다음은 string.replace() 메서드의 예입니다.



자바스크립트
let string = 'GeeksForGeeks'; let newstring = string.replace('GeeksForGeeks', 'GfG'); console.log(newstring);>

산출
GfG>

설명:

  • 문자열string>값이 할당됩니다'GeeksForGeeks'>.
  • 그만큼replace()>메서드가 호출됩니다.string>, 하위 문자열 검색'GeeksForGeeks'>그리고 그것을로 대체'GfG'>.
  • 전체 문자열부터'GeeksForGeeks'>일치하는 경우 다음으로 대체됩니다.'GfG'>.
  • 새로운 문자열'GfG'>변수에 할당됩니다newstring>.
  • newstring>콘솔에 기록됩니다.

문자열 바꾸기() 메서드 예:

여기서 GeeksForGeeks 문자열의 내용은 gfg로 대체됩니다.

자바스크립트
 // Assigning a string  let string = 'GeeksForGeeks is a CS portal';    // Calling replace() method  let newstring = string.replace(/GeeksForGeeks/, 'gfg');    // Printing replaced string  console.log(newstring);>

산출
gfg is a CS portal>

설명:

  • 문자열string>값이 할당됩니다'GeeksForGeeks is a CS portal'>.
  • 그만큼replace()>메서드가 호출됩니다.string>, 정규 표현식을 사용하여 하위 문자열 일치'GeeksForGeeks'>그리고 그것을로 교체'gfg'>.
  • 첫 번째 발생'GeeksForGeeks'>문자열은 다음으로 대체됩니다.'gfg'>.
  • 수정된 문자열이 변수에 할당됩니다.newstring>.
  • 교체된 문자열newstring>콘솔에 기록됩니다.

문자열 바꾸기() 메서드 예:

다음은 string.replace() 메서드의 예입니다.

자바스크립트
// Taking a regular expression let re = /GeeksForGeeks/; // Taking a string as input let string = 'GeeksForGeeks is a CS portal'; // Calling replace() method to replace // GeeksForGeeks from string with gfg let newstring = string.replace(re, 'gfg'); // Printing new string with replaced items console.log(newstring);>

산출
gfg is a CS portal>

설명:

  • 정규식re>하위 문자열과 일치하도록 정의되었습니다.'GeeksForGeeks'>.
  • 문자열string>값이 할당됩니다'GeeksForGeeks is a CS portal'>.
  • 그만큼replace()>메서드가 호출됩니다.string>, 정규식 사용re>하위 문자열과 일치시키기 위해'GeeksForGeeks'>그리고 그것을로 교체'gfg'>.
  • 첫 번째 발생'GeeksForGeeks'>문자열은 다음으로 대체됩니다.'gfg'>.
  • 수정된 문자열이 변수에 할당됩니다.newstring>.
  • 교체된 문자열newstring>콘솔에 기록됩니다.

문자열의 여러 위치에서 동일한 단어를 바꿀 수도 있습니다. 글로벌 대체품으로 알려져 있습니다.



문자열 바꾸기() 메서드 예:

이 예에서는 문자열에서 다양한 유사한 단어를 바꾸는 방법을 설명합니다.

자바스크립트
// Assigning a string let string = 'GeeksForGeeks is a CS portal.' +  'In GeeksForGeeks we can learn multiple languages.' +  'geeksForGeeks is a great place.'; // Calling replace() method let newstring = string.replace(/GeeksForGeeks/g, 'Gfg'); // Printing replaced string console.log(newstring);>

산출
Gfg is a CS portal.In Gfg we can learn multiple languages.geeksForGeeks is a great place.>

설명:

  • 여러 줄 문자열string>하위 문자열이 여러 번 할당됩니다.'GeeksForGeeks'>.
  • 그만큼replace()>메서드가 호출됩니다.string>, 전역 플래그(/g>) 하위 문자열의 모든 항목과 일치합니다.'GeeksForGeeks'>대소문자를 구분하여 다음으로 대체합니다.'Gfg'>.
  • 다음의 모든 발생'GeeksForGeeks'>문자열에서 다음으로 대체됩니다.'Gfg'>.
  • 수정된 문자열이 변수에 할당됩니다.newstring>.
  • 교체된 문자열newstring>콘솔에 기록됩니다.

JavaScript 문자열 바꾸기() 메서드 – 사용 사례:

1. JavaScript 문자열 바꾸기All() 메서드

자바스크립트 모두 다 바꿔() 메서드는 문자열의 모든 일치 항목을 지정된 문자열 또는 정규식으로 바꾼 후 새 문자열을 반환합니다. 이 작업 후에 원래 문자열은 변경되지 않고 그대로 유지됩니다.

2. JavaScript에서 문자열의 모든 항목을 바꾸는 방법은 무엇입니까?

JavaScript에서 문자열의 모든 항목을 바꾸려면 정규식 및 g 플래그와 함께 교체() 메서드를 사용할 수 있습니다.

우리는 Javascript 문자열 메소드의 전체 목록을 가지고 있습니다. 이를 확인하려면 다음을 수행하십시오. 자바스크립트 문자열 전체 참조 기사.

지원되는 브라우저:

  • Google 크롬 1 이상
  • 엣지 12 이상
  • 파이어폭스 1 이상
  • 인터넷 익스플로러 5.5 이상
  • 오페라 4 이상
  • 사파리 1 이상