How to remove specific words with javascript

Asked

Viewed 12,621 times

5

How can I remove specific page title values?

Title: STACKOVERFLOW

REMOVE: STACK

RESULT: OVERFLOW

  • This may help: https://www.regex101.com/

3 answers

11


Knowing that you can access the page title via document.title then you can transform that string and set the title again with document.title = novaString;.

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

Example:

var titulo = document.title;
document.title = titulo.replace('STACK', ''); // vai mudar o titulo removendo "STACK"

2

In the case of Javascript you can use the method substring!
With the method substring we take a piece of the string, 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);

Exit:

 overflow

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

 var titulo = document.title;

1

If you wanted to search for a specific value in a string, regardless of your Index you can use the suaString.indexOf('STACK'), if you find something such method will return the index from the beginning of the string (e.g.: 4), if you do not find your answer you 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'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.