Replace back slash (\) with forward slash (/) [duplicate] - javascript

This question already has answers here:
Path with backslashes to path with forward slashes javascript
(2 answers)
How to replace backward slash to forward slash using java?
(3 answers)
Closed 5 years ago.
I need to replace this path: C:\test1\test2
into this:
C:/test1/test2
I am using jquery but it doesn't seem to work
var path = "C:\test1\test2";
var path2 = path.replace("\", "//");
How should it be done?

You have to escape to backslashes.
var path = "C:\\test1\\test2";
var path2 = path.replace(/\\/g, "/");
console.log(path2);

Your original string is in the wrong format, as '\t' inside it is for a tab symbol. Please change it (may be from the server side) to this:
var path = "C:\\test1\\test2";
so your code could change to this:
var path = "C:\\test1\\test2";
var path2 = path.replace(/\\/g, '/');

Related

Converting backslash to forward slash in Javascript from GetSeletedText() [duplicate]

This question already has answers here:
Path with backslashes to path with forward slashes javascript
(2 answers)
How to replace backward slash to forward slash using java?
(3 answers)
Closed 5 years ago.
I need to replace this path: C:\test1\test2
into this:
C:/test1/test2
I am using jquery but it doesn't seem to work
var path = "C:\test1\test2";
var path2 = path.replace("\", "//");
How should it be done?
You have to escape to backslashes.
var path = "C:\\test1\\test2";
var path2 = path.replace(/\\/g, "/");
console.log(path2);
Your original string is in the wrong format, as '\t' inside it is for a tab symbol. Please change it (may be from the server side) to this:
var path = "C:\\test1\\test2";
so your code could change to this:
var path = "C:\\test1\\test2";
var path2 = path.replace(/\\/g, '/');

How can I replace or delete a character from a string in React JS like normal Javascript? [duplicate]

This question already has answers here:
Replace all occurances in javascript
(4 answers)
Closed 2 years ago.
I have a string that contains multiple '-'. I want to remove the all '-'. I tried the following way but it didn't work.
var str = "baa7f3b17ffc-4216-bfbc-8e9f70f26984"
var new_str = str.replace('-', '')
How can I remove or replace all the '-'? Is there any simple function for that?
Remove globally and remove the second parenthesis
var str = "baa7f3b17ffc-4216-bfbc-8e9f70f26984";
var new_str = str.replace(/-/g, '');
console.log(new_str);

How can I find a specific string within a path using regex? [duplicate]

This question already has an answer here:
Regex to find string between slashes
(1 answer)
Closed 4 years ago.
I have the following path:
/s/STRINGINEED/abcdef/
How should I structure my regex to match STRINGINEED as a result?
/s/ is a fixed path, so I would like to get any string between /s/ and the following /.
To get the string coming after /s/ in the path, you can use the following regex:
\/s\/([\w]+)\/
Demo:
const regex = /\/s\/([\w\d]+)\//gm;
const str = `/s/STRINGINEED/abcdef/`;
console.log(regex.exec(str)[1]);
Another option is to use .split() method:
str.split('/s/')[1].split("/")[0]
Demo:
var str = '/s/STRINGINEED/abcdef/';
console.log(str.split('/s/')[1].split("/")[0]);
You might execute:
\/s\/([^\/]*)
and then use the first group matched.
Demo: https://regex101.com/r/MaGIlD/2

How to replace the backward slash from string? [duplicate]

This question already has answers here:
JavaScript: A BackSlash as part of the string
(3 answers)
Closed 4 years ago.
What is wrong with the following code?
Expected output : substr1#substr2#substr3
var str = "substr1\substr2\substr3"
// it works if I use the double slash "\\" in thestring but not with single.
console.log(str.replace(/\\/g, "#"));
Your initial string itself do not have a backslash. To verify check the snippet below:
var str = "substr1\sustr2\substr3"
console.log(str);
The actual output you expect can be obtain by first escaping the backslash and then replacing it with #:
var str = "substr1\\sustr2\\substr3"
console.log(str.replace(/\\/g, "#"));

Replacing "/" with ":" using jQuery [duplicate]

This question already has answers here:
Fastest method to replace all instances of a character in a string [duplicate]
(14 answers)
Closed 6 years ago.
New to jQuery & JavaScript.
I have
var x = location.pathname;
(ex: /abc/collection/tea/green/index.php)
Like this I have various pathname retrieved using location.pathname.
I want to replace all "/" in the pathname with ":" (I mean a / with a :) and also I don't want the .php which is at the end. Any help please.
This has nothing to do with jQuery. You can use JavaScript to replace your string.
Like this:
var x = location.pathname;
x = x.replace(/\//g, ":");
or just
var x = location.pathname.replace(/\//g, ":");
You can also use the same method to remove the ".php" by adding this:
x = x.replace(/\.php$/i, "");
(assuming you only need to replace it once at the end)
Basically you have to use the regex version of str.replace() and the g (global) switch to do a replace all. Use the str.replace(/texthere/g, "replacement") pattern to replace more than one occurrence - just remember to properly escape 'texthere' so characters don't conflict.

Categories

Resources