Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [JavaScript] zamiana pojedyńczego backslasha w stringu
stellatus
post 2.07.2020, 12:05:55
Post #1





Grupa: Zarejestrowani
Postów: 196
Pomógł: 0
Dołączył: 9.03.2017

Ostrzeżenie: (0%)
-----


Jak zamienić w JS stringa: "C:\Users\Reti\Desktop\folder" na "C:/Users/Reti/Desktop/folder"? Tutaj nie ma żadnego problemu: https://regexr.com/57o0k, ale to nie działa w konsoli. Nie ma też problemu gdy string wyjściowy wygląda tak: "C:\\Users\\Reti\\Desktop\\folder" (https://stackoverflow.com/questions/42855606/replace-back-slash-with-forward-slash).

Ten post edytował stellatus 2.07.2020, 12:07:34
Go to the top of the page
+Quote Post
trueblue
post 2.07.2020, 12:50:40
Post #2





Grupa: Zarejestrowani
Postów: 6 761
Pomógł: 1822
Dołączył: 11.03.2014

Ostrzeżenie: (0%)
-----


Twój string jest w niepoprawnej postaci. Pojedynczy backslash z literą tworzy kod ucieczki (znane kody to, np. \n, \r, \t).


--------------------
Go to the top of the page
+Quote Post
stellatus
post 2.07.2020, 13:19:04
Post #3





Grupa: Zarejestrowani
Postów: 196
Pomógł: 0
Dołączył: 9.03.2017

Ostrzeżenie: (0%)
-----


Źle zadałem pytanie. Chodziło mi konkretnie o sytuację, w której backslash jest wpisywany do inputa. Okazuje się, że regex działa wtedy normalnie: https://codepen.io/reti/pen/gOPoPNQ
Kod
    <div>Folder path: <input type="text" id="folder-path" size="100" oninput="foo()" value="C:\Users\Reti\Desktop\folder">
        </div><br><br>

    <div id="text"></div>

    <script>
        foo = () => {
          var folderPath = document.getElementById('folder-path').value
       document.getElementById('text').innerHTML = folderPath.replace(/\\/g, "/")
        }
        foo()
          
    </script>
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 19.04.2024 - 19:42