Throwback Friday: Renumber my ZX spectrum BASIC listing
JavaScript (ES6) 177

Edit Added the (costly) scan for the next valid line number

l=&gt;l.split`
`.map((x,i)=&gt;([,n,t]=x.match(/(\d+)(.*)/),l[n]=10*-~i,t),l=[]).map((x,i)=&gt;10*-~i+x.replace(/(UN |GO TO |UB )(\d+)$/,(a,b,c)=&gt;(l.some((v,i)=&gt;i&lt;c?0:a=b+v),a))).join`
`


TEST



f=l=&gt;
  l.split`\n`
  .map((x,i)=&gt;([,n,t]=x.match(/(\d+)(.*)/),l[n]=10*-~i,t),l=[])
  .map((x,i)=&gt;10*-~i+x.replace(/(UN |GO TO |UB )(\d+)$/,(a,b,c)=&gt;(l.some((v,i)=&gt;i&lt;c?0:a=b+v),a)))
  .join`\n`
        
//TEST
console.log=x=&gt;O.textContent+=x+'\n'
  
test=`1 REM "A rearranged guessing game"
2 INPUT A: CLS
3 INPUT "Guess the number ", B
10 IF A=B THEN PRINT "Correct": STOP
100 IF A&lt;B THEN GO SUB 125
120 IF A&gt;B THEN GO SUB 122
121 GO TO 3
125 PRINT "Try again"
126 RETURN`
console.log(test+'\n\n'+f(test))
&lt;pre id=O&gt;&lt;/pre&gt;