避免转义序列并在 Javascript 中拆分字符串

我需要拆分以下字符串并获取值。已尝试使用JS 字符串拆分方法,但它不适用于反斜杠 split()。请帮我。提前致谢。


Input string = "11,22;0:0/0\0}0#0&"


Output:

, => 11,

; => 22

: => 0

/ => 0

\ => 0

} => 0

# => 0

& => 0


MMMHUHU
浏览 73回答 2
2回答

白衣染霜花

我参考了很多阅读资料做了修改。希望您觉得这个有帮助。此解决方案中还打印了带有转义字符的“0”。请检查一下。let string = "26,67;4:79/9\0}0&";&nbsp; &nbsp; string = encodeURI(string);&nbsp; &nbsp; string = string.replace("%0", "&");&nbsp; &nbsp; string = decodeURI(string);&nbsp; &nbsp; numbers = string.split(/,|;|:|\/|\\|}|&/);&nbsp; &nbsp; finalList = [];&nbsp; &nbsp; for (let i = 0; i < numbers.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (numbers[i] != "")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; finalList.push(parseInt(numbers[i]));&nbsp; &nbsp; }&nbsp; &nbsp; console.log(finalList);

湖上湖

它对我来说很好用。请尝试以下代码。let string = "26,67;4:79/9\0}0&";let arr = string.split(/,|;|:|\/|\\|}|&/);&nbsp; &nbsp; &nbsp;// ["26", "67", "4", "79", "9 ", "0", ""]谢谢...
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript