Adscend

Click Here



Click Here

Tuesday

String Function replace Tutorial

String Function replace()

Object: String
Method or Function: replace(arg1, agr2)
Syntax: string.replace(arg1, agr2)

The replace() function searches for a match in a string, and replaces the matched substring with a new substring.
The replace() function takes two parameters.
The first argument is what we are looking or searching for, and the second argument is what we will replace the found ones with.


Example: 1

<script type="text/javascript">
var string="hello everybody, my name is Aspell";
document.write(string.replace(/Aspell/,"Sky"));
</script>


Example: 2

<script type="text/javascript">
var string="hello everybody, my name is Aspell";
document.write(string.replace("Aspell","Sky"));
</script>


You can also perform a case-insensitive search:

Example: 3

<script type="text/javascript">
var string="hello everybody, my name is Aspell";
document.write(string.replace(/aspell/i,"Sky"));
</script>


You can also perform global, case-insensitive search:

Example: 4

<script type="text/javascript">
var string="hello everybody, my name is Aspell.";
string=string + " Again I say, my name is Aspell.";
document.write(string.replace(/aspell/gi,"Sky"));
</script>




No comments:

Post a Comment