0

I am sending a string variable from spring boot controller to JavaScript and my string is basically a file path with single backslash. I am reading the path in JavaScript but it is ready single backslash as a escape character or something.

<script type="text/javascript">
    var path = "S:\\CD\2020\ABCDEF"
    alert(path)
</script>

The output I am getting is S:\CD‚0ABCDEF in the alert

I want "S:\CD\2020\ABCDEF"

0

3 Answers 3

1

\ is used to escape characters. Add an extra \ to get past it:

var path = "S:\\C\\2020\\ABCDEF"
    alert(path)

Sign up to request clarification or add additional context in comments.

3 Comments

I am getting this variable from spring boot controller and I have no control over this variable. I cant directly change it.
Change it in your front end right? Make a copy and add extra ``. Is that not possible?
is there any way to use replace function and replace single backslash with double backslash?
1

You need to do

var path = "S:\\CD\\2020\\ABCDEF"

a backslash is an escape character so you need to escape the backslash.

If you don't control it, try to use String.raw like so:

alert(String.raw`S:\\CD\2020\ABCDEF`)

5 Comments

I am getting this variable from spring boot controller and I have no control over this variable. I cant directly change it.
You could try using String.raw, see edit
is there any way to use replace function and replace single backslash with double backslash?
The issue is that the string itself doesn't actually have the backslash once it's in the variable, you need to get at it before it "becomes" a variable
String.raw also does not work, now its now showing the alert
1

In JavaScript \ is an escape character used to denote the beginning of special sequences.

These sequences include \n, \t and even Unicode characters. When you want to use the \ character on its own, write two like this \\.

So in your case:

  • We can use String.raw to stop JavaScript from parsing escape sequences.
  • And also .replaceAll to change all occurrences of the \\ sequence.

var path = String.raw`S:\\CD\2020\ABCDEF`

let singular = (String.raw`\a`).slice(0, 1)
let double = String.raw`\\`

path = path.replaceAll(double, singular)

alert(path)

As a function:

function getString() {
   return String.raw `S:\\CD\2020\ABCDEF`
} // This function can be replaced with the function you are using to get the path from the user.

function replaceEscapes(rawString) {
  let singular = (String.raw `\a`).slice(0, 1)
  let double = String.raw `\\`

  return rawString.replaceAll(double, singular)
}

alert(
  replaceEscapes(getString())
)

6 Comments

I am getting this variable from spring boot controller and I have no control over this variable. I cant directly change it.
is there any way to use replace function and replace single backslash with double backslash?
@MutahirKiani I've made an edit to my post, this should let you replace the characters programmatically.
Thanks man this solved my problem. Have a nice day :)
@MutahirKiani No problem 👍, If it worked make sure to mark the solution as accepted.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.