我知道这个问题已经在这个平台上被问过很多次了,但我无法理解如何使用下面的字符串对反斜杠字符 (\) 进行分割。
student\boy
我尝试按 \
进行分割,但它给出了 undefined
。
function getSecondPart(str) {
return str.split("\\")[1];
}
console.log(getSecondPart("student\boy"));
我发现它正在考虑 \b
(退格键),因此如果我指定 str.split("\b")[1]
,它会给出 oy
但我需要子字符串作为 boy
。
最佳答案
字符串中的反斜杠不被视为反斜杠,而是被视为特殊字符“\b”。如果要在字符串中使用反斜杠,则需要使用双反斜杠。
"student\\boy" // will return "student\boy"
getSecondPart("student\\boy") // will return "boy"
关于javascript - 在 JavaScript 中获取反斜杠 (\) 后的字符串值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61628959/