Rails:将特定字符串后的字符大写

发布于 2024-12-08 06:23:38 字数 335 浏览 0 评论 0原文

使用:Rails 3.0.3。

我有一个字符串可以看作是这个:

<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>

我希望每个字符后面的任何

;
> ! ? 。要大写。基本上让上面的字符串看起来不错。

有标准功能吗?我应该怎么办?

附:尝试将有问题的字符加粗,但没有成功......

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>

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. Tried to bold the characters in question, but it didn't work...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

等数载,海棠开 2024-12-15 06:23:38

使用 Nokogiri,您可以在文本周围添加任何标记,并且它会在
上中断。等

ng = Nokogiri::HTML.fragment("<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>")
ng.traverse{|n| (n.content = n.content.gsub(/(.*?)([\.|\!|\?])/) { " #{$1.strip.capitalize}#{$2}" }.strip) if n.text?}
ng.to_s

给出:

"<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>"

我使用了两次 strip,首先是因为 stop/question/exclamation 之后的句子将有空格,否则不会大写,然后为了将间距放回去,我在每个句子之前添加了一个空格 - 第二个 strip 删除了生成的空格最终输出的开始。

Using Nokogiri you can have any markup around the text, and it breaks on the <br /> etc.

ng = Nokogiri::HTML.fragment("<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>")
ng.traverse{|n| (n.content = n.content.gsub(/(.*?)([\.|\!|\?])/) { " #{$1.strip.capitalize}#{$2}" }.strip) if n.text?}
ng.to_s

gives :

"<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>"

I used strip twice, first because a sentence after stop/question/exclamation will have white space and not capitalize otherwise, then to put the spacing back I added a space before each sentence - the second strip removes the generated space at the start of the final output.

逆光飞翔i 2024-12-15 06:23:38

离开 Niklaos 的回答,我稍微改进了它,以考虑到渲染的 HTML 中的句点并保持间距完整。

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

编辑:将标点符号添加到单个字符类中,捕获所有空白之间的内容以保留间距。干净多了。

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}"}

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

ヅ她的身影、若隐若现 2024-12-15 06:23:38

Capitalize 是您正在寻找的方法。尽管你必须为每个句子都这样做。如果需要的话,你可以使用 nokogiri 轻松解析它

capitalize is the method you are looking for. although you have to do it for each sentence. You can easily parse that with nokogiri if you have to though

转角预定愛 2024-12-15 06:23:38

嗯,我觉得是时候调用超级正则表达式了:)

str.gsub(/(\<p\>|\<br \/\>|! |\? )(.{1})/) {"#{$1}#{$2.capitalize}"}

这应该可以完成工作。

注意:可以对此进行改进以获得更大的灵活性。

Hum I feel like it's time to call super Regex :)

str.gsub(/(\<p\>|\<br \/\>|! |\? )(.{1})/) {"#{$1}#{$2.capitalize}"}

That should to the job.

Note: this can be improved to have more flexibility.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文