How to remove specific words with javascript

5

How can I remove specific values from the page title?

Title : STACKOVERFLOW

REMOVE : STACK

RESULT : OVERFLOW

    
asked by anonymous 04.02.2015 / 00:10

3 answers

10

Knowing that you can access the title of the page via document.title you can then transform that string and re-define the title with document.title = novaString; .

This will have SEO implications that you should take into account ... (this is a subject already addressed in other questions).

Example:

var titulo = document.title;
document.title = titulo.replace('STACK', ''); // vai mudar o titulo removendo "STACK"
    
04.02.2015 / 00:18
2

In the case of JavaScript you can use the substring method! With the substring method we take a piece of the string, we pass the initial position followed by the number of characters that must be extracted.

Ex:

var titulo = "Stackoverflow";
var resultado = titulo.substring(5, 15);

console.log(resultado);

Output:

 overflow

To get the title of the page, use document.title

 var titulo = document.title;
    
04.02.2015 / 00:31
1

If you wanted to search for a specific value in a string, regardless of its indexes, you can use suaString.indexOf('STACK') , if you find something, that method will return the index at the beginning of the string (ex: 4 ), if you do not find your answer will be -1.

Example:

var busca = "STACK";
var suaString = "STACKOVERFLOW";
var indexBusca = suaString.indexOf(busca);
// a variavel indexBusca irá armazenar o index do retorno, se for diferente de -1 é porque a string foi encontrada em 'suaString'
    
04.02.2015 / 02:02