Javascript 在javascript中替换'\'n

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14072313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:39:10  来源:igfitidea点击:

replace '\'n in javascript

javascriptreplace

提问by Moin

I'm trying to do replace in JavaScript using:

我正在尝试使用以下方法在 JavaScript 中进行替换:

r = "I\nam\nhere";
s = r.replace("\n"," ");

But instead of giving me

但不是给我

I am here

我在这里

as the value of s, It returns the same.

作为 的值s,它返回相同的值。

Where's the problem??

问题出在哪里??

回答by Pablo Jomer

As stated by the others the greedy flag is missing for your regular expression. The correct expression should be some thing like what the others gave you.

正如其他人所说,您的正则表达式缺少贪婪标志。正确的表达应该类似于其他人给你的表达。

var r = "I\nam\nhere";
var s = r.replace(/\n/g,' ');

I would like to point out the difference from what was going on from the start. you were using the following statements

我想指出与从一开始发生的事情的不同之处。您使用了以下语句

var r = "I\nam\nhere";
var s = r.replace("\n"," ");

The statements are indeed correct and will replace one instance of the character \n. It uses a different algorithm. When giving a String to replace it will look for the first occurrence and simply replace it with the string given as second argument. When using regular expressions we are not just looking for the character to match we can write complicated matching syntax and if a match or several are found then it will be replaced. More on regular expressions for JavaScript can be found here w3schools.

这些陈述确实是正确的,将替换字符 \n 的一个实例。它使用不同的算法。当给出要替换的字符串时,它将查找第一次出现并简单地将其替换为作为第二个参数给出的字符串。使用正则表达式时,我们不仅要查找要匹配的字符,还可以编写复杂的匹配语法,如果找到一个或多个匹配项,则将替换它。有关 JavaScript 正则表达式的更多信息,请访问w3schools

For instance the method you made could be made more general to parse input from several different types of files. Due to differences in Operating system it is quite common to have files with \n or \r where a new line is required. To be able to handle both your code could be rewritten using some features of regular expressions.

例如,您创建的方法可以更通用,以解析来自几种不同类型文件的输入。由于操作系统的差异,在需要换行的地方有带有 \n 或 \r 的文件是很常见的。为了能够同时处理您的代码,可以使用正则表达式的某些功能进行重写。

var r = "I\ram\nhere";
var s = r.replace(/[\n\r]/g,' ');

回答by David says reinstate Monica

The problem is that you need to use the gflag to replace allmatches, as, by default, replace()only acts on the first match it finds:

问题是您需要使用该g标志来替换所有匹配项,因为默认情况下,replace()仅对它找到的第一个匹配项起作用:

var r = "I\nam\nhere",
    s = r.replace(/\n/g,' ');

To use the gflag, though, you'll have to use the regular expression approach.

g但是,要使用该标志,您必须使用正则表达式方法。

Incidentally, when declaring variables please use var, otherwise the variables you create are all global, which can lead to problems later on.

顺便说一句,声明变量时请使用var,否则你创建的变量都是全局的,这可能会导致以后出现问题。

回答by Grijesh Chauhan

use s = r.replace(/\\n/g," ");

s = r.replace(/\\n/g," ");

Get a reference:

获取参考

The "g" in the javascript replace code stands for "greedy" which means the replacement should happen more than once if possible

javascript 替换代码中的“g”代表“贪婪”,这意味着如果可能,替换应该发生多次

回答by doctorless

.replace()needs the global match flag:

.replace()需要全局匹配标志:

s = r.replace(/\n/g, " ");

回答by Daniel Antonio Nu?ez Carhuayo

You can use:

您可以使用:

var s = r.replace(/\n/g,' ').replace(/\r/g,' ');

because diferents SO use diferents ways to set a "new line", for example: Mac Unix Windows, after this, you can use other function to normalize white spaces.

因为不同所以使用不同的方式来设置“新行”,例如:Mac Unix Windows,在此之后,您可以使用其他功能来规范空格。

回答by Margaret Kalashnik

It's working for me:

它对我有用:

var s = r.split('\n').join(' ');

回答by goshant meher

Just use \\\nto replace it will work.

只需使用\\\n替换它就可以了。

r.replace("\\n"," ");

回答by estellezg

The solution from hereworked perfect for me:

这里的解决方案对我来说非常完美:

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