首页 > 解决方案 > 为什么在 node.js 中使用 split 时我得到 '+' 符号而不是 ','

问题描述

我有一个 .txt 文件,其中包含一些短语(拾取行),每个短语都在一个新行中。
这是一个样本

Does this rag smell like chloroform to you?
I have amnesia - do I come here often?
Your lips look lonely. Let me introduce them to mine
Your shirt has to go, but you can stay

我想创建一个数组,这样每一行都是这个数组中的一个元素。
所以我在节点上做了这个

const fs = require('fs')

let a = fs.readFileSync('database.txt', 'utf-8')
b = a.split('/n')

console.log(b)

但我得到的只是我的测试,行之间有一个加号。

标签: javascriptnode.jstextsplit

解决方案


你必须用反斜杠'\n'分割行

    b = a.split('\n')

推荐阅读