In javascript his can be done with a single compound statement. By using indexOf function we can determine the position of the tab character by this expression
tabPosition=theText.indexOf("\t")
which gives us a zero-indexed position or -1 if there are no remaining tabs.
The number of spaces we need to add will be at most four, but we should trim that by the remainder of tabPosition divided by 4 (i.e. tabPosition%4), so that the total number of characters after the replacement is an exact multiple of 4 characters. i.e. we should replace the tab character by this string expression:
(" ").substr(0,4-tabPosition%4)
There may be more than one tab in the text so we should repeat the replacement as long as tabPosition isn't -1. Putting that all together gives:
while((tabPosition=theText.indexOf("\t"))!=-1)theText=theText.replace("\t",(" ").substr(0,4-tabPosition%4));
That single line is all we need to covnvert theText into a tabbed-by-indiviudal-space-characters version. Obviously it's more useful as a function, but why not add a bit more functionality so that tabs can be 2, 3, 4 ....12 character positions apart? We just need to replace the number 4 in our coumpund statement by a variable, and make whitespace string contain more space characters.
function tabs2Spaces(theText,tabSpacing)
{
while((tabPosition=theText.indexOf("\t"))!=-1)theText=theText.replace("\t",(" ").substr(0,tabSpacing-tabPosition%tabSpacing));
return theText;
}
document.writeln ("Using tab positions 4 characters apart:")
document.writeln(tabs2Spaces("\tHello World",4));
document.writeln(tabs2Spaces(" \tHello World",4));
document.writeln(tabs2Spaces(" \tHello World",4));
document.writeln(tabs2Spaces(" \tHello World",4));
document.writeln(tabs2Spaces(" \tHello World",4));
document.writeln(tabs2Spaces(" \tHello World",4));
document.writeln(tabs2Spaces(" \tHello World",4));
document.writeln(tabs2Spaces(" \tHello World",4));
document.writeln ("Using tab positions 6 characters apart:")
document.writeln(tabs2Spaces("\tHello World",6));
document.writeln(tabs2Spaces(" \tHello World",6));
document.writeln(tabs2Spaces(" \tHello World",6));
document.writeln(tabs2Spaces(" \tHello World",6));
document.writeln(tabs2Spaces(" \tHello World",6));
document.writeln(tabs2Spaces(" \tHello World",6));
document.writeln(tabs2Spaces(" \tHello World",6));
document.writeln(tabs2Spaces(" \tHello World",6));
document.writeln ("Using tab positions 9 characters apart:")
document.writeln(tabs2Spaces("\tHello World",9));
document.writeln(tabs2Spaces(" \tHello World",9));
document.writeln(tabs2Spaces(" \tHello World",9));
document.writeln(tabs2Spaces(" \tHello World",9));
document.writeln(tabs2Spaces(" \tHello World",9));
document.writeln(tabs2Spaces(" \tHello World",9));
document.writeln(tabs2Spaces(" \tHello World",9));
document.writeln(tabs2Spaces(" \tHello World",9));
.... which outputs this text ...
Using tab positions 4 characters apart:
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Using tab positions 6 characters apart:
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Using tab positions 9 characters apart:
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World