gpt4 book ai didi

javascript - 用/使用javascript替换字符串中的all\with

转载 作者:行者123 更新时间:2023-11-28 15:36:04 24 4
gpt4 key购买 nike

我目前正在研究 Javascript。
我有一个变量,其取值 D:\Abc\xyz\mno\rst\uvw.inc我需要将上面变量中的所有 \ 替换为 / 。我收到错误:SyntaxError:未终止的字符串文字

有人可以帮我解决这个问题吗?
代码如下:

<html>
<head>
</head>
<body>
<table>
<tr>
<td>File Name </td>
<td><Input type="text" id="file_name" size="100" onblur="getFilePath(this.value);"> </td>
<td><Input type="text" id="for_file_name" size="100"></td>
</tr>
</table>
<script>
function getFilePath(var_input) {
alert("Input: "+var_input);
var myArray = var_input.split("\");
var myStr = myArray.join('/');
alert(myStr)
}
</script>

</body>
</html>

最佳答案

您应该转义字符串中的所有反斜杠字符:

var myArray = var_input.split('\\');

另外,请确保您不使用 Unicode 引号 (''),而是使用普通引号 (''),如 中那样加入参数:

// ----------------------v-v
var myStr = myArray.join(‘/’);

关于javascript - 用/使用javascript替换字符串中的all\with,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25619566/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com