The general solution for rerolling once is to use a helper function like this:
function: ROLL:n reroll BAD:s as REROLL:d { if ROLL = BAD { result: REROLL } result: ROLL }
You can assign the result of this function to a custom die, and then roll as many of them as you want:
X: [d6 reroll {1,2} as d6] output [highest 3 of 4dX]
For re-rolling an unlimited number of times, we can use a trick from this answer by Carcer and pass the "empty die" d{} as the REROLL parameter, like this:
Y: [d6 reroll {1,2} as d{}] output [highest 3 of 4dY]
When summing up the results of the function, AnyDice will simply ignore these empty dice, as if the rolls that resulted in them had never happened.
However, for simple dice like a single d6, it's also possible to just directly define a custom die that excludes the faces to be rerolled, as described in Alex P's and Dale M's answers:
output [highest 3 of 4d{3..6}]
The general reroll trick is more useful in cases where the input "die" to be rerolled is already the result of some complex rolling procedure, so that the probabilities of different rolls are not all the same (like they are with a simple d6).
For example, if you wanted to calculate, say, the distribution of ability scores obtained by rolling 4d6, dropping the lowest die and then rerolling the whole score until it's at least 10, you could use this code:
output [[highest 3 of 4d6] reroll {3..9} as d{}]