August 19, 2009

Javascript Tutorial 15: indexOf, match, replace

indexOf


var word ="this is just a sample text that i have created";
document.write(word.indexOf("text"));


It will give you a number and that is that from zero, in what character does the word start. text starts in 22. If you are searching for a different element that is not here, then it will javascript will give you a value of -1.

match


var word ="this is just a sample text that i have created";
document.write(word.match("text"));


If the word that you are trying to find is not available, then it will say "null". If it matches, then it will show that text.

replace


var word ="this is just a sample text that i have created";
document.write(word.replace(/text/,"words"));

This will find the word "text" and replace it with the word "words".

, ,



Powered by ScribeFire.

No comments:

Post a Comment