String functions to find Contains and to Replace in Javascript




Below I described two string javascript functions that I often forget. 

"If string Contains" in Javascript you can determine using "indexOf()" command.

var test = 'Hello World'; 
if( test.indexOf('World') >= 0){ 
    alert('Found world'); 
}

If substring is not found, the result is -1. That’s why it’s better to compare with "-1":

if(test.indexOf('World') = -1){ 
   alert('Found world'); 
}

 

To replace substring you should use String.Replace command. The syntax to replace the word "World" to nothing ('') is like this:

var test = 'Hello World'; 
newtest = test.Replace('World', '');

alert(newtest);

The result is "Hello ".




1 Comment »

  1. Oi, Eu acho este site ótimo . Eu adorei .
    Irei voltar novamente e até já marquei nos favoritos. Sucesso!!

    Comment by france.org.br — December 5, 2018 @ 4:41 am

RSS feed for comments on this post. TrackBack URL

Leave a comment





MarkiMarta.com. Notes of web-specialist
Since 2009
18+