admin管理员组文章数量:1431955
I am having trouble removing line breaks from my text in javascript. Here is an example of the data I am working with:
0: "Christian Pulisic"
1: "↵"
2: "From Wikipedia, the free encyclopedia"
3: "↵"
4: "Christian Pulisic"
5: "↵"
6: "Personal information"
7: "↵"
8: "Full name Christian Mate Pulisic[1]"
Obviously, the line spaces/breaks or whatever you call them, are polluting the data I am getting.
I cannot get the following function to recognize the line breaks and replace it with a "" which I then can very easily remove from the array through another function I run my data through.
This is the code I am currently using that is not working:
for (i in cleanArray){
cleanArray[i].replace(/(\r\n|\n|\r)/gm,"")
};
console.log(cleanArray);
I am having trouble removing line breaks from my text in javascript. Here is an example of the data I am working with:
0: "Christian Pulisic"
1: "↵"
2: "From Wikipedia, the free encyclopedia"
3: "↵"
4: "Christian Pulisic"
5: "↵"
6: "Personal information"
7: "↵"
8: "Full name Christian Mate Pulisic[1]"
Obviously, the line spaces/breaks or whatever you call them, are polluting the data I am getting.
I cannot get the following function to recognize the line breaks and replace it with a "" which I then can very easily remove from the array through another function I run my data through.
This is the code I am currently using that is not working:
for (i in cleanArray){
cleanArray[i].replace(/(\r\n|\n|\r)/gm,"")
};
console.log(cleanArray);
Share
Improve this question
asked Feb 8, 2016 at 3:12
LanceLance
1233 silver badges14 bronze badges
1
- 1 The replace() method returns a new string, it doesn't change the original one. – nnnnnn Commented Feb 8, 2016 at 3:15
2 Answers
Reset to default 3cleanArray[i].replace(/(\r\n|\n|\r)/gm,"")
will return a new string. Furthermore, strings are immutable in JavaScript. Change it to
for(var i = 0; i < cleanArray.length; ++i)
cleanArray[i] = cleanArray[i].replace(/(\r\n|\n|\r)/gm,"")
Assuming the characters shown as "↵"
are actual newlines
Use Array#filter
with String#trim
var newArr = arr.filter(el => el.trim());
本文标签: javascriptRemove line breaksspaces from arrayStack Overflow
版权声明:本文标题:javascript - Remove line breaksspaces from array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745591389a2665220.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论