拆分来自 HTML 文本区域的字符串会导致 GOLANG 中出现意外结果

问题描述 投票:0回答:1

我有一个字符串变量str,它保存的值是

my textarea

来自 HTML texarea

我正在尝试用新行分割字符串

fmt.Println("befor split:")
    fmt.Println(str)
    x := strings.Split(str, "\n")
fmt.Println("after split:")
    fmt.Println(x)

这就是我得到的

the result i got

html string go split
1个回答
0
投票

不同类型的行结束符:根据平台的不同,换行符可以表示为 (视窗), (Unix/Linux),或 (老麦克)。

const textareaValue = document.getElementById("yourTextareaId").value;
const lines = textareaValue.split(/\r?\n/); // This will handle both \r\n and \n
© www.soinside.com 2019 - 2024. All rights reserved.