Ruby Regexp Unescape

May not be a big deal to write it yourself…but I wrote this and was quite useful, where I had to do un-escaping of a Regex Pattern for some reasons…

class Regexp
def unescape
source = self.source.split('')
escape_on = false
unescaped_string = source.inject([]) {|r, char|
if char == "\\" and escape_on == false
escape_on = !escape_on
else
r << char
escape_on = false
end
r
}.join
end
end

Leave a comment