The previous example showed how to intersect a line with a polygon, but the situations can be more complicated, as shown in this example.
The solution is using the lineSplit function for each part of the multilinestring and selecting the ones that fit the needs. The first part can be inside or outside the polygon, depending on the first point. Detecting this, is easy to get only the needed parts.