Rails:将某些字符串后的字符大写 [英] Rails: Capitalize characters after certain string

查看:184
本文介绍了Rails:将某些字符串后的字符大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用:Rails 3.0.3。

Using: Rails 3.0.3.

我有一个字符串,可以看做这个:

I have a string that could be seen as this one:

<p>hello, how are you? oh, that's nice! i am glad you are fine. i am too.<br />i am glad to have met you.</p>

我想要< p> < br /> ! ?。资本化。基本上使上面的字符串看起来不错。

I want each character following any of <p> <br /> ! ? . to be capitalized. Basically make the string above look good.

有没有标准的功能?我该怎么办?

Is there a standard functionality for that? What should I do?

ps。试图将有问题的字符加粗,但它没有工作...

ps. Tried to bold the characters in question, but it didn't work...

推荐答案

在渲染的HTML中稍微改进它以考虑周期和保持间距完整。

Going off Niklaos answer, I've improved it slightly to take into account the period and keeping spacing intact in the rendered HTML.

str.gsub(/(\<p\>|\<br \/\>|[?!.])([\s]*)([[:alpha:]]{1})/) {"#{$1}#{$2}#{$3.capitalize}"}

字符在单个字符类中,捕获所有在空格之间以保留间距。更清洁。

Added punctuation characters to be in a single character class, captured all in between whitespace to preserve spacing. Much cleaner.

这篇关于Rails:将某些字符串后的字符大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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