Friday, 9 May 2014

Javascript String Replace using while

<script>
var x = "<div><a onclick='hello()' href='#' </a>AAAAAAAAA<a onclick='hello()' href='#' </a>BBBBBBBBBBBBB<a onclick='hello()' href='#' </a>CCCCCCCCCC</div>";
document.write(x);
var start = x.indexOf('<a');
var end = x.indexOf('</a>') + 4;
var final = x.slice(start, end);
while (start > -1) {
x = x.replace(final, ' Welcome ');
start = x.indexOf('<a');
end = x.indexOf('</a>') + 4;
final = x.slice(start, end);
}
document.write(x);
</script>