简单的正则表达式——用空格替换下划线 [英] simple regex -- replace underscore with a space

查看:75
本文介绍了简单的正则表达式——用空格替换下划线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我正在编写我的第一个 Rails 应用程序,我正在尝试用空格替换传入 id 名称中的下划线,如下所示:

Hey, I'm writing my first Rails app, and I'm trying to replace the underscores form an incoming id name with spaces, like this:

之前:test_string

before: test_string

之后:测试字符串

我该怎么做?对不起,如果这是一个愚蠢的问题,我对正则表达式不是很熟悉...

How can I do this? Sorry if this is a bit of a dumb question, I'm not very familiar with regular expressions...

推荐答案

糟糕,我真的让它工作了——只是忘了更新变量名 :P

Whoops, I actually had it working--just forgot to update the variable name :P

我正在使用这个:

@id = params[:id]
@title = @id.gsub("_", " ")

这篇关于简单的正则表达式——用空格替换下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆