What does it do?

gsub(/^/, "\t" * num) 

Which character is being replaced?

+4
source share
2 answers

The character is not replaced, it just inserts the num tabs at the beginning, so you can say that it replaces the โ€œbeginning of lineโ€ zero-width marker. Whoever wrote this would be better with something more similar:

 tabbed = "\t" * num + original 

Regular expression is really not the right tool for simple string concatenation.

Explanation: If you expect your line to contain multiple lines, use:

 gsub(/^/, "\t" * num) 

to prefix all tabbed lines is a sensible thing and less noisy than splitting, prefix and reattaching. If you only expect to deal with one line per line, then a simple string concatenation would be the best choice.

+7
source

^ means "start of line" in regex syntax, so num characters will be inserted at the beginning of each line. Technically, you can say that it replaces the empty line at the beginning of each line.

+5
source

All Articles