0

I am currently working on Javascript.
I have a variable withich take value D:\Abc\xyz\mno\rst\uvw.inc I need to replace all \ with / from above variable. I am getting the error: SyntaxError: unterminated string literal

Can someone please help me with this issue ?
Code is given below:

<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>

2 Answers 2

2

You should escape all backslash characters in strings:

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

Also make sure that you don't use the Unicode quotes (‘’) instead of the normal quotes (''), as you have in join arguments:

// ----------------------v-v
var myStr = myArray.join(‘/’);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Vision, I have already corrected the question. btw Thanks for your assistance. Cheers. Have a good day. :)
@raavan You are welcome. BTW, as proposed by #sergiomse it will be wise to use 'global' string replace instead of split-join combination, as it will work much faster.
Sure mate.. I will have a try on that too.. Cheers.. :)
2

Try this:

var_input = var_input.replace(/\\/g, "/");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.