<textarea id="ta1" rows="10" onchange="applytabtospaces_delayed();" onkeyup="applytabtospaces_delayed();"></textarea>
<textarea id="ta2" rows="10" readonly></textarea>
<br/>
convert tabs to <input id="tabspaces" type="number" value="4" size=2 min=1 max=100 step=1 style="width:36px;text-align:right;padding:0px;border:0px;" /> spaces<br/>
<span class="checkboxbox"><input id="updatescroll" type="checkbox" style="padding:0px;border:0px;background-color:#888;color:#f00;" checked /></span> updatescroll<br/>
<span class="checkboxbox"><input id="updateresize" type="checkbox" style="padding:0px;border:0px;background-color:#888;color:#f00;" checked /></span> updateresize<br/>
<h4 class="clickable" onclick="$('#exampleinpre').toggle();">+ example in <pre> tags:</h4>
<div id="exampleinpre" style="display:none;">
text below edited in a 4-space-tab editor, the 2 columns look odd in a default PRE or TEXTAREA container, because it will use 8-space-tabs:
<pre id="test1">
1234567890123456789012345678901234567890
0
1
2
3
a 1 2
aa 1b 2
aaa 1bb 2
aaaa 2cc
example text, 2 columns edited with a 4-space-tab editor:
2 columns, seperated by 2 tabs (2nd column is at pos 8)
a b
aa b
aaa b
aaaa b
aaaaa b
aaaaaa b
aaaaaaa b
aaaaaaaa c
aaaaaaaaa c
</pre>
same as above, but copy from above replacing tabs with 0-4 spaces (depending on where the tab is in the line), so everything is aligned in a "4-space-grid":
<pre id="test2">
</pre>
"&emsp;" == " " is still a fixed unit width, no tab magic happens:
<pre>
 1
1 2
22 3
333 4
0
 1
  2
   3
    4
</pre>
[ <span class="checkboxbox"><input type="checkbox"/></span> checkbox styled border test ]<br/>
</div>
// string repeat implemented using Russian Peasant multiplication
String.prototype.repeat = function (n) {
if (n<1) return '';
var accum = '', c=this;
for (; n; n >>=1) {
if (1&n) accum += c;
c += c;
}
return accum;
}
String.prototype.untabify = function(tabWidth) {
// replace all starting tabs with tabWidth spaces each (for all tabs on the beginning of the line):
tabWidth = tabWidth || 4;
return this.replace(/^\t+/gm, function(tabs) { return ' '.repeat(tabWidth * tabs.length)} );
}
String.prototype.tabstospaces_replacefirsttabwithspaces = function(tabWidth){
// replace the first tab
var firsttabpos = this.indexOf('\t');
if(firsttabpos < 0){
return this;
}else if(firsttabpos == 0){
return ' '.repeat(tabWidth) + this.substr(1);
}else{
// replace a tab at pos x with y spaces: y = 8 - (x % 8)
var nspaces = tabWidth - (firsttabpos % tabWidth);
return this.substr(0, firsttabpos) + ' '.repeat(nspaces) + this.substr(firsttabpos + 1);
}
}
String.prototype.tabstospaces = function(tabWidth){
// cut up string in lines and for each line:
// for each tab encountered, calculate the char position from the start of the line, and replace it with (tabWidth - (tabcharpos % tabWidth) ) spaces
var firsttabpos, n,line,lines = this.split('\n');
for(n = 0; n < lines.length ; n++){
line = lines[n];
// firsttabpos = line.indexOf('\t');
while(line.indexOf('\t') >= 0){
line = line.tabstospaces_replacefirsttabwithspaces(tabWidth);
}
lines[n] = line;
}
return lines.join('\n');
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
// (function(window){
function bodyonload(){
var p1,p2;
p1 = document.getElementById('test1');
p2 = document.getElementById('test2');
p2.innerHTML = ('' + p1.innerHTML).tabstospaces(4);
/*
var obj, objs =...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.