const regex = /((^0)\s+|(^1)\s+|(^2)\s+|(^3)\s+|(^4)\s+|(^5)\s+)((((.{4})(.+)))|((.{4})))/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('((^0)\\s+|(^1)\\s+|(^2)\\s+|(^3)\\s+|(^4)\\s+|(^5)\\s+)((((.{4})(.+)))|((.{4})))', 'gm')
const str = `0 @I0066@ INDI
1 NAME Joan Adrienne /Hatch/
2 GIVN Joan Adrienne
2 SURN Hatch
2 SOUR @S0045@
3 DATA
4 TEXT Birth date: 30 May 1935 Birth place: Hennepin, Minnesota
2 SOUR @S0131@
3 PAGE Source Citation: Year: 1940; Census Place: Faribault, Rice, Minnesota; Roll: T627_1953; Page: 8B; Enumeration District: 66-9A
3 DATA
4 TEXT residence date: 1 Apr 1940 residence place: Faribault, Rice, Minnesot
5 CONC a, United States birth date: May 30, 1935 birth place: Minneapolis
5 CONC , Hennepin, Minnesota, USA Name: Joan Adrienne Hatch
3 NOTE @N0121@
2 SOUR @S0635@
3 DATA
4 TEXT Marriage date: 29 Dec 1953 Marriage place: Cook County, IL
1 SEX F
1 BIRT
2 DATE 30 MAY 1935
2 PLAC Minneapolis, Hennepin, Minnesota, USA
2 SOUR @S0045@
3 DATA
4 TEXT Birth date: 30 May 1935 Birth place: Hennepin, Minnesota
2 SOUR @S0131@
3 PAGE Source Citation: Year: 1940; Census Place: Faribault, Rice, Minnesota; Roll: T627_1953; Page: 8B; Enumeration District: 66-9A
3 DATA
4 TEXT residence date: 1 Apr 1940 residence place: Faribault, Rice, Minnesot
5 CONC a, United States birth date: May 30, 1935 birth place: Minneapolis
5 CONC , Hennepin, Minnesota, USA Name: Joan Adrienne Hatch
3 NOTE @N0125@
1 DEAT
2 DATE 14 DEC 1989
2 PLAC Nyack, Rockland, New York, USA
1 RESI
2 DATE 1 APR 1940
2 PLAC Faribault, Rice, Minnesota, USA
2 SOUR @S0131@
3 PAGE Source Citation: Year: 1940; Census Place: Faribault, Rice, Minnesota; Roll: T627_1953; Page: 8B; Enumeration District: 66-9A
3 DATA
4 TEXT residence date: 1 Apr 1940 residence place: Faribault, Rice, Minnesot
5 CONC a, United States birth date: May 30, 1935 birth place: Minneapolis
5 CONC , Hennepin, Minnesota, USA Name: Joan Adrienne Hatch
3 NOTE @N0127@
1 RESI
2 DATE 1 APR 1940
2 PLAC Faribault, Rice, Minnesota, USA
2 SOUR @S0131@
3 PAGE Source Citation: Year: 1940; Census Place: Faribault, Rice, Minnesota; Roll: T627_1953; Page: 8B; Enumeration District: 66-9A
3 DATA
4 TEXT residence date: 1 Apr 1940 residence place: Faribault, Rice, Minnesot
5 CONC a, United States birth date: May 30, 1935 birth place: Minneapolis
5 CONC , Hennepin, Minnesota, USA Name: Joan Adrienne Hatch
3 NOTE @N0129@
1 FAMC @F1196@
1 FAMS @F0129@
1 FAMS @F0260@
1 CHAN
2 DATE 31 MAR 2017
3 TIME 22:03:40`;
const subst = `\n $1 | $2 | $3 | $4 | $5 | $6 | $7 | $8 | $9 | $10 | $11 | $12 | $13 | $14 | |`;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions