问题
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...
回答1:
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.
回答2:
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.
回答3:
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
回答4:
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.
来源:https://stackoverflow.com/questions/7650753/rails-capitalize-characters-after-certain-string