HOWTO · JavaScript

Retirar um caractere de uma string em JavaScript

Demonstra diferentes formas de remover um carácter de uma string em JavaScript

O JavaScript tem diferentes métodos para remover um carácter específico de uma string. Iremos introduzir como remover um carácter de uma string em JavaScript.

Utilize o replace() Método Com Expressão Regular em JavaScript

Utilizamos o método replace() com a expressão regular para remover todas as instâncias do carácter especificado numa string em JavaScript.

JavaScript replace()Sintaxe de Expressão Regular

replace(/regExp/g, '');

Exemplo:

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
       How to remove all instances of the specified character in a string? 
    </title> 
</head> 
  
<body> 
    <h1> 
        DelftStack 
    </h1> 
      
    <b> 
        How to remove all instances of the specified character in a string? 
    </b> 
      
    <p>The original string is DelftStack</p> 
      
    <p> 
        New Output is:  
        <span id="outputWord"></span> 
    </p> 
  
    <button onclick="removeCharacterFromString()"> 
        Remove Character 
    </button> 
      
    <script type="text/javascript"> 
        const removeCharacterFromString = () => {
            originalWord = 'DelftStack'; 
            newWord = originalWord.replace(/t/g, ''); 
  
            document.querySelector('#outputWord').textContent  
                    = newWord; 
        } 
    </script> 
</body>  
</html>  

Resultado:

The original string is DelftStack

New Output is: DelfSack

Remover um Caracteres Especificados no Índice Dado em JavaScript

Quando precisamos de remover um carácter quando temos mais de uma instância deste carácter numa string, por exemplo, remover o carácter t de uma string DelftStack, podemos utilizar o método slice() para obter duas strings antes e depois do índice dado e concatená-las.

Exemplo:

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
       How to remove Specified Character at a Given Index in a string? 
    </title> 
</head> 
  
<body> 
    <h1> 
        DelftStack 
    </h1> 
      
    <b> 
        How to remove Specified Character at a Given Index in a string? 
    </b> 
      
    <p>The original string is DelftStack</p> 
      
    <p> 
        New Output is:  
        <span id="outputWord"></span> 
    </p> 
  
    <button onclick="removeCharacterFromString(5)"> 
        Remove Character 
    </button> 
      
    <script type="text/javascript"> 
        const removeCharacterFromString = (position) => {
            originalWord = 'DelftStack'; 
            newWord = originalWord.slice(0, position - 1) 
            + originalWord.slice(position, originalWord.length); 
  
            document.querySelector('#outputWord').textContent  
                    = newWord; 
        } 
        
    </script> 
</body>   
</html>  

Remover Primeira Instância de Caracteres de uma String em JavaScript

Podemos utilizar o método replace() sem uma expressão regular para remover apenas a primeira instância de um caractere de uma string em JavaScript. Passamos o caracter a ser removido como primeiro argumento e a string vazia '' como segundo argumento.

Exemplo:

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
       How to remove First Instance of Character in a string? 
    </title> 
</head> 
  
<body> 
    <h1> 
        DelftStack 
    </h1> 
      
    <b> 
        How to remove First Instance of Character in a string? 
    </b> 
      
    <p>The original string is DelftStack</p> 
      
    <p> 
        New Output is:  
        <span id="outputWord"></span> 
    </p> 
  
    <button onclick="removeCharacterFromString()"> 
        Remove Character 
    </button> 
      
    <script type="text/javascript"> 
        const removeCharacterFromString = () => {
            originalWord = 'DelftStack'; 

            newWord = originalWord.replace('t', ''); 

            document.querySelector('#outputWord').textContent  
                    = newWord; 
        } 
        
    </script> 
</body> 
</html>