raavan
raavan

Reputation: 88

Replacing all \ with in string with / using javascript

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>

Upvotes: 0

Views: 45

Answers (2)

sergiomse
sergiomse

Reputation: 785

Try this:

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

Upvotes: 2

VisioN
VisioN

Reputation: 145398

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(‘/’);

Upvotes: 2

Related Questions