Jet fragmentation in pp and PbPb collisions at a centre-of-mass energy of 2.76 TeV per nucleon pair was studied using data collected with the CMS detector at the LHC. Fragmentation functions are constructed using charged-particle tracks with transverse momenta p(T) > 4 GeV/c for dijet events with a leading jet of p(T) > 100 GeV/c. The fragmentation functions in PbPb events are compared to those in pp data as a function of collision centrality, as well as dijet-p(T) imbalance. Special emphasis is placed on the most central PbPb events including dijets with unbalanced momentum, indicative of energy loss of the hard scattered parent partons. The fragmentation patterns for both the leading and subleading jets in PbPb collisions agree with those seen in pp data at 2.76 TeV. The results provide evidence that, despite the large parton energy loss observed in PbPb collisions, the partition of the remaining momentum within the jet cone into high-p(T) particles is not strongly modified in comparison to that observed for jets in vacuum.