Remove spaces when using OCR

Is there a way to remove spaces in OCR extracted text? Whenever I use that and paste it the result has spaces at the beginning and the end.

DemoPDFTest_with_OCR has some examples for this.

In this macro you find this regular expression. It removes spaces and line breaks. Use it with ExecuteScript:

return ${q}.replace(/( |\n|\r)/gm, "")