如何使用 Javascript 删除 IE 中的换行符?

发布于 2024-12-04 20:46:42 字数 1581 浏览 0 评论 0原文

我正在尝试使用 JavaScript 删除换行符。它适用于除 Internet Explorer 之外的所有浏览器(在 ie7、ie8 中测试)。我读了很多帖子,但找不到问题的解决方案。

Var someText = "Here’s your text.\n It has line breaks that need to be removed.\rUsing Javascript.\r\n"; 

someText = someText.replace(/(\r\n|\n|\r)/gm,"");

我还能尝试什么?

*已编辑*****

这里有更多代码和更好的解释我的问题:

function checkLength(fn) {
    var fn = document.getElementById(fn);
    var fnb = fn.value;
    var fnb = fnb.replace(/(\r\n|\n|\r)/gm,"");
    var len = fnb.length;
    ...

我想做的是计算文本区域中的字符数。我必须同时使用 Javascript 和 PHP 进行计算,并且由于换行符,PHP 和 Javascript 永远不会得出相同的数字。当删除换行符时,除了在 Internet Explorer 中(当我用 Javascript 计算它时)之外,一切都很好。 fnb.replace 不会更改 Internet Explorer 中的任何字符数,因此我确信它不会正确删除换行符。在所有其他浏览器中都很好,在删除 JavaScript 中的换行符后我可以看到计数器的差异。仅在 ie 中它不会改变任何事情。我已经尝试了一些事情以及您下面的建议,并且删除换行符之前和之后的字符数在 ie 中是相同的。

///////// 我的答案 //////////////////////////////////////

function checkLength(fn) {
    var fn = document.getElementById(fn);
    var fnb = fn.value;
    var fnb = fnb.replace(/(\r\n|\n|\r)/gm,"");
    var len = fnb.length;
...

像托马拉克一样说,我的逻辑可以改进 - 抱歉,我是 JavaScript 和 JavaScript 新手。编程。也许托马拉克不会犯任何错误,但我相信其他人都会犯错误。我们必须犯错误才能学习。

Internet Explorer 不喜欢

var fn = document.getElementById(fn);
var fnb = fn.value;

我必须将其更改为:

var fnb = document.getElementById(fn).value;

即使它不合逻辑,它应该可以工作。它确实适用于除 ie 之外的所有浏览器。这是一个错误。

这就是我一直在寻找的答案。

I am trying to remove linebreaks with JavaScript. It works in all browsers except Internet Explorer (tested in ie7, ie8). I have read a lot of posts but couldn't find a solution to the problem.

Var someText = "Here’s your text.\n It has line breaks that need to be removed.\rUsing Javascript.\r\n"; 

someText = someText.replace(/(\r\n|\n|\r)/gm,"");

What else can I try?

* EDITED *****

Here's more code and a better explanation of my problem:

function checkLength(fn) {
    var fn = document.getElementById(fn);
    var fnb = fn.value;
    var fnb = fnb.replace(/(\r\n|\n|\r)/gm,"");
    var len = fnb.length;
    ...

What I am trying to do is calculate the number of chars in a textarea. I had to calculate with both Javascript and PHP and because of the linebreaks, PHP and Javascript never came to the same number. When removing the linebreaks, it is all good except in Internet Explorer (when I calculate it with Javascript). fnb.replace doesn't change anything in Internet Explorer for the character count so that is why I am sure it does not remove the linebreaks correctly. In all other browsers it is fine, I can see the difference in the counter after removing the linebreaks in javascript. Only in ie it doesn't change a thing. I have tried a couple of things as well as your suggestions below and the char count before removing the linebreaks and after is the same in ie.

///////// MY ANSWER /////////////////////////////////////

function checkLength(fn) {
    var fn = document.getElementById(fn);
    var fnb = fn.value;
    var fnb = fnb.replace(/(\r\n|\n|\r)/gm,"");
    var len = fnb.length;
...

Like Tomalak said, my logic could be improved - Sorry for being new to JavaScript & programming. Maybe Tomalak doesn't make any mistakes but I'm sure everyone else does. We have to make mistakes to learn.

Internet Explorer didn't like

var fn = document.getElementById(fn);
var fnb = fn.value;

I had to change it to:

var fnb = document.getElementById(fn).value;

Even if it wasn't logical, it should have worked. It did work in all browsers except ie. It's a bug.

That was the answer I was looking for.

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

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

发布评论

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

评论(3

月光色 2024-12-11 20:46:42

试试这个:

someText = someText.replace(/[\r\n]+/gm,"");

Try this:

someText = someText.replace(/[\r\n]+/gm,"");
も让我眼熟你 2024-12-11 20:46:42

事实上,正则表达式至少在IE8.0中工作得很好。

真正的问题是你写的是 Var 而不是 var; Firefox 可以让你逃脱惩罚,但 IE 不行。

In fact, the regular expression works just fine in at least IE8.0.

The real problem is that you wrote Var instead of var; Firefox lets you get away with it, but IE doesn't.

因为看清所以看轻 2024-12-11 20:46:42

您不需要 m 标志,但您应该用空格替换而不是什么也不替换。

someText = someText.replace(/[\r\n]+/g,' ');

You don't need the m flag, but you ought to replace with a space instead of nothing.

someText = someText.replace(/[\r\n]+/g,' ');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文