如何从rails中的代码中删除一个新行(\ n)?

时间:2022-10-30 09:37:18

I currently have the following code (jQuery) in my view, and when title has a \n character then it breaks my js.

我目前在我的视图中有以下代码(jQuery),当title具有\ n字符时,它会破坏我的js。

$("#t").html("#{title.html_safe}");

The following works but I think it's somewhat a hack:

以下工作,但我认为这有点黑客:

$("#t").html("#{title.gsub("\n","")}");

4 个解决方案

#1


4  

The method you are looking for is String#chomp. It will remove any carriage return characters from the end of your string.

您正在寻找的方法是String#chomp。它将从字符串末尾删除任何回车字符。

$("#t").html("#{title.chomp}");

As always, only use html_safe if you are completely sure the title variable is safe... especially since you are using it within a Javascript file! I did not include it in my code example because I just couldn't bring myself to do it. Plus, see the note on APIdock in regards to using html_safe on a variable that could be nil. I would suggest only using it on a string literal.

和往常一样,如果您完全确定标题变量是安全的,那么只使用html_safe ...特别是因为您在Javascript文件中使用它!我没有在我的代码示例中包含它,因为我无法让自己去做。另外,请参阅APIdock上关于在可能为nil的变量上使用html_safe的说明。我建议只在字符串文字中使用它。

EDIT:

编辑:

If there is a chance that your title may contain quotes that need to be escaped before used in Javascript (for instance '"Winter is Coming", I say' which will interpolate into your javascript as .html(""Winter is Coming", I say");) then you should also use the escape_javascript method as suggested in the link @mu is too short provided.

如果您的标题可能包含在Javascript中使用之前需要转义的引号(例如“冬天来了”,我说'将插入到您的javascript中为.html(“”冬天来了“,我说“);)那么你也应该使用链接中建议的escape_javascript方法@mu太短了。

$("#t").html("#{j(title.chomp)}");

If title = '"Winter is Coming"' then the above code would produce.

如果title ='“Winter is Coming”'那么上面的代码就会产生。

$("#t").html("\"Winter is Coming\"");

If there is also a chance that there is a carriage return in the middle of the title string, then String#gsub is the way to go.

如果在标题字符串的中间还有一个回车符,那么String#gsub就是你要走的路。

$("#t").html("#{j(title.gsub(/[\n\r]/, " "))}");

I put a space in the second parameter of gsub so 'Winter\nis\nComing' would become 'Winter is Coming' and not 'WinterisComing'.

我在gsub的第二个参数中放了一个空格,所以'Winter \ nis \ nComing'将成为'Winter is Coming'而不是'WinterisComing'。

#2


1  

I'm not sure if you are going to get less hacky

我不确定你是否会变得更少hacky

If you still want to have new lines, you could use simple_format

如果您仍想要新行,可以使用simple_format

i.e. $("#t").html("#{simple_format title}");

ie $(“#t”)。html(“#{simple_format title}”);

http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-simple_format

http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-simple_format

Though that also wraps it in a <p> tag.

虽然它也包含在

标签中。

#3


0  

You could try:

你可以尝试:

$("#t").html("#{title.html_safe}".chomp);

or shorter:

或更短:

$("#t").html(title.html_safe.chomp);

This removes trailing newlines.

这会删除尾随换行符。

If you want to remove leading and trailing spaces you may replace chomp with strip.

如果要删除前导和尾随空格,可以使用strip替换chomp。

If your text has newlines inside the string, the gsub solutions seems to be the right solution, but you don't need the #{...}:

如果您的文本在字符串中有换行符,那么gsub解决方案似乎是正确的解决方案,但您不需要#{...}:

$("#t").html(title.gsub("\n",""));

#4


0  

So many wrong answers. Use escape_javascript.

这么多错误的答案。使用escape_javascript。

#1


4  

The method you are looking for is String#chomp. It will remove any carriage return characters from the end of your string.

您正在寻找的方法是String#chomp。它将从字符串末尾删除任何回车字符。

$("#t").html("#{title.chomp}");

As always, only use html_safe if you are completely sure the title variable is safe... especially since you are using it within a Javascript file! I did not include it in my code example because I just couldn't bring myself to do it. Plus, see the note on APIdock in regards to using html_safe on a variable that could be nil. I would suggest only using it on a string literal.

和往常一样,如果您完全确定标题变量是安全的,那么只使用html_safe ...特别是因为您在Javascript文件中使用它!我没有在我的代码示例中包含它,因为我无法让自己去做。另外,请参阅APIdock上关于在可能为nil的变量上使用html_safe的说明。我建议只在字符串文字中使用它。

EDIT:

编辑:

If there is a chance that your title may contain quotes that need to be escaped before used in Javascript (for instance '"Winter is Coming", I say' which will interpolate into your javascript as .html(""Winter is Coming", I say");) then you should also use the escape_javascript method as suggested in the link @mu is too short provided.

如果您的标题可能包含在Javascript中使用之前需要转义的引号(例如“冬天来了”,我说'将插入到您的javascript中为.html(“”冬天来了“,我说“);)那么你也应该使用链接中建议的escape_javascript方法@mu太短了。

$("#t").html("#{j(title.chomp)}");

If title = '"Winter is Coming"' then the above code would produce.

如果title ='“Winter is Coming”'那么上面的代码就会产生。

$("#t").html("\"Winter is Coming\"");

If there is also a chance that there is a carriage return in the middle of the title string, then String#gsub is the way to go.

如果在标题字符串的中间还有一个回车符,那么String#gsub就是你要走的路。

$("#t").html("#{j(title.gsub(/[\n\r]/, " "))}");

I put a space in the second parameter of gsub so 'Winter\nis\nComing' would become 'Winter is Coming' and not 'WinterisComing'.

我在gsub的第二个参数中放了一个空格,所以'Winter \ nis \ nComing'将成为'Winter is Coming'而不是'WinterisComing'。

#2


1  

I'm not sure if you are going to get less hacky

我不确定你是否会变得更少hacky

If you still want to have new lines, you could use simple_format

如果您仍想要新行,可以使用simple_format

i.e. $("#t").html("#{simple_format title}");

ie $(“#t”)。html(“#{simple_format title}”);

http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-simple_format

http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-simple_format

Though that also wraps it in a <p> tag.

虽然它也包含在

标签中。

#3


0  

You could try:

你可以尝试:

$("#t").html("#{title.html_safe}".chomp);

or shorter:

或更短:

$("#t").html(title.html_safe.chomp);

This removes trailing newlines.

这会删除尾随换行符。

If you want to remove leading and trailing spaces you may replace chomp with strip.

如果要删除前导和尾随空格,可以使用strip替换chomp。

If your text has newlines inside the string, the gsub solutions seems to be the right solution, but you don't need the #{...}:

如果您的文本在字符串中有换行符,那么gsub解决方案似乎是正确的解决方案,但您不需要#{...}:

$("#t").html(title.gsub("\n",""));

#4


0  

So many wrong answers. Use escape_javascript.

这么多错误的答案。使用escape_javascript。