2

StringReplace["abbaabbaa", "ab" -> "X"] will output XbaXbaa, but StringReplace["abbaabbaa", "ab" -> Style["X", Red]] outputs

enter image description here

It is weird, as why there are horizontal lines between the replaced characters and other characters?

xzczd
  • 65,995
  • 9
  • 163
  • 468
Y. zeng
  • 567
  • 2
  • 6

1 Answers1

5

Cannot find an exact duplicate at the moment, but the solution is mentioned at least in the following 3 posts:

https://mathematica.stackexchange.com/a/77842/1871

https://mathematica.stackexchange.com/a/213639/1871

https://mathematica.stackexchange.com/a/246756/1871

StringReplace["abbaabbaa", "ab" -> ToString[Style["X", Red], StandardForm]]

enter image description here

xzczd
  • 65,995
  • 9
  • 163
  • 468
  • I was using StandardForm like ToString[StringReplace["abbaabbaa", "ab" -> Style["X", Red]], StandardForm] before resorting to the embarassing workaround I posted. – Syed Sep 20 '23 at 05:12