Converting backslash to forward slash in Javascript from GetSeletedText() [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

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);

Prevent back slash escaping from the string in Javascript [duplicate]

This question already has answers here:
Escaping backslash in string - javascript
(5 answers)
Closed 3 years ago.
I have a code which replaces the back slashes as empty string
var data = '<strong>Welcome</strong>\(x = {-b';
document.write(data);
I am getting result like this:
Welcome(x = {-b
I am expecting result like this without modifying string value
Welcome\(x = {-b
I used the same string to display in an id as html content.
document.getElementById("test").innerHTML = '<strong>Welcome</strong>\(x = {-b';
It always replacing slashes by empty string. the string.split method did not helped me to solve this
var data = '<strong>Welcome</strong>\\(x = {-b';
document.write(data);
try this...

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, "#"));

Replace back slash (\) with forward slash (/) [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, '/');

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