The regular expression matches if a string contains the persian phrase with one or more whitespace characters around it (both at the front and back).
Then it replaces it with the string \1
. The \1
refers to the first matched element. So, it removes all the whitespace around the string and adds one space after the element.
Example
I am taking the value test
instead of the Parsi phrase, because unicode wasn't working out.
newline = " test "
=> " test "
newline.gsub!(/\s+(test)\s+/,'\1 ')
=> "test "