1 Problem Code

Take a look at the code below. What do you think it will output?

let a = 'public_uploads_1631175317736.png';
let arr = [
    {site:"public_uploads_1631175317736.png"},
    {site:"public_uploads_1631175317736.png"},
]
let b = "\";
​
arr.forEach(item => {
    let str = item.site.replace(/_/g,b)
    console.log("str:",str)
    item.site = str
})
​
console.log("a:",a.replace(/_/g,b)); 
console.log("arr:",arr)
Copy the code

Tell the story, hear my story, weep!

2 What happened

When I write my code, I want to save the avatar uploaded by the user on the server, and then I found that there are more paths to save the picture in the database, and then I will operate, upload the UPLOads component using Antd in the front end, because it is written to play, so I use Node to write the server, I used a multer middleware to save the image, and then I could get the saved path like public\uploads\1631175317736. PNG. Since MY computer is Windows, I can see that the path separator uses a backslash “\”.

Then I wanted to save it in the database! It turns out that because the backslash is an escape, the database will swallow it. Then I looked it up and asked, and finally I used the underscore instead of the backslash to save it to the database. When I used it, I turned it back, so the database stored is public_uploads_1631175317736.png.

Then I began to use, although this is a bit of trouble, but the first time, first write out the function again, I so comfort themselves, the results will encounter the above problems. String test escaped well, traversal to assign value to the object does not transfer, so I also specially try map for etc. It turns out that when the loop iterates over the assignment to the object, it just doesn’t escape. Even if you test the code above, you can see that STR is always a backslant bar, and when assigned to item.site, it’s two backslashes. How could this, this, this be more ridiculous? I wondered if I had found a bug in VScode. Then I brazenly asked in the group, obviously we have not encountered such silly batch of problems. I spent the rest of the evening in the library staring at this code, testing it, tinkering with it, making no progress. When going to the library is closed on time, thought couldn’t stay white, the night I was cheeky and group a simple reply just the brother of the question, consult modestly, brother a operation fierce like a tiger, and then told me that the data returned to the client shows only a backslash, I tried, found that it happens, it’s really touched! Thank you quickly, good mood. Let me try node xxx.js >> a.txt ‘, I’ll try, ok, same result as terminal display. But I ultimately came to a similar conclusion, which I think is a display problem. So I went back to the dormitory and happily continued to write down the code.

Ok, astonishing reversal of the play, I suddenly found that, although the Windows system files address delimiters using backslash “|”, but write code file segmentation address is with slashes “/”! So when I first save it in the database, I just replace it with a backslash. There wouldn’t have been a problem. I really, the brain clot is about to give air out!

Really, give my best wishes to you, I hope you write code easily, unimpeded, a thousand miles!

3 Supplementary Screenshots

A screenshot of the test results is attached at the end

Terminal:

Output to a file:

Send to client: