红宝石 |正则表达式报价()函数
Regexp#quote() : quote()是一个 Regexp 类方法,它转义任何在正则表达式中具有特殊含义的字符。
Syntax: Regexp.quote()
Parameter: Regexp values
Return: escapes any characters that would have special meaning in a regular expression.
示例 #1:
# Ruby code for Regexp.quote() method
# declaring Regexp value
reg_a = Regexp.quote('/a/')
# declaring Regexp value
reg_c = Regexp.quote('\*?{}.')
# quote method
puts "Regexp quote form : #{reg_a}\n\n"
puts "Regexp quote form : #{reg_c}\n\n"
输出 :
Regexp quote form : /a/
Regexp quote form : \\\*\?\{\}\.
示例 #2:
# Ruby code for Regexp.quote() method
# declaring Regexp value
reg_a = Regexp.quote('/geeks/')
# declaring Regexp value
reg_b = Regexp.quote('/(?.)(?.)(?.)/')
# declaring Regexp value
reg_c = Regexp.quote('\*?????{}.')
# quote method
puts "Regexp quote form : #{reg_a}\n\n"
puts "Regexp quote form : #{reg_b}\n\n"
puts "Regexp quote form : #{reg_c}\n\n"
输出 :
Regexp quote form : /geeks/
Regexp quote form : /\(\?\.\)\(\?\.\)\(\?\.\)/
Regexp quote form : \\\*\?\?\?\?\?\{\}\.