Im using ruby and rails to automatically create a filename from the name of the product and the product's variant-type. Using .gsub, the filename will be lowercase and have special characters (spaces, ', -) removed. Ive got most of it working but I can't seem to get it to remove double quotes.
我使用ruby和rails自动从产品名称和产品的变体类型创建文件名。使用.gsub,文件名将为小写,并删除特殊字符(空格,', - )。我已经完成了大部分工作,但我似乎无法删除双引号。
This works for single quotes:
这适用于单引号:
"'"
But this doesn't work for double-quotes:
但这不适用于双引号:
'"'
Here's my code:
这是我的代码:
filepath_name = product.name+"_"+variant_type.gsub(/ /,'').gsub("'", "").gsub("-", "").gsub('"', '').downcase+".mpg"
1 个解决方案
#1
3
You could just use a regexp to remove anything but ascii characters like:
您可以使用正则表达式删除除ascii字符之外的任何内容:
variant_type.gsub!(/[^0-9A-Za-z.\-]/, '')
and modify it to suit your needs. You can use rubular for a reference.
并根据您的需要进行修改。您可以使用rubular作为参考。
#1
3
You could just use a regexp to remove anything but ascii characters like:
您可以使用正则表达式删除除ascii字符之外的任何内容:
variant_type.gsub!(/[^0-9A-Za-z.\-]/, '')
and modify it to suit your needs. You can use rubular for a reference.
并根据您的需要进行修改。您可以使用rubular作为参考。