How to replace backward slash with forward in js [duplicate] - javascript

This question already has answers here:
JavaScript: A BackSlash as part of the string
(3 answers)
Closed 3 years ago.
I'm trying to replace forward slash with backward with below code but the result is not as expected, What's the expected second parameter for replace in this case?
var path = 'C:\Users\abc\AppData\Local\Programs\Python\Python37\python.exe';
path.replace(/\\/g, "/");
console.log(path)
result:
"C:UsersabcAppDataLocalProgramsPythonPython37python.exe"

Your regex is fine but variable declaration needs double backslash because single backslash is interpreted as escape character:
var path = 'C:\\Users\\abc\\AppData\\Local\\Programs\\Python\\Python37\\python.exe';
path = path.replace(/\\/g, "/");
console.log(path);
//=> C:/Users/abc/AppData/Local/Programs/Python/Python37/python.exe
If you want to avoid using \\ in assignment then you can use String.raw
var path = String.raw`C:\Users\abc\AppData\Local\Programs\Python\Python37\python.exe`;

path.replace(/\134/g,"/");
As a regular expression. \134 is the octal representation of a backslash

Related

Defining regex in Javascript [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 1 year ago.
I need to define below regex in Javascript:
(\bin region\b)(?!.*\1).+?(?=<)
I tried like below but it looks like not working:
var reg = new RegExp ('(\bin region\b)(?!.*\1).+?(?=<)');
Although the Atom tool matches the regex in the target string, JavaScript code is returning blank.
I am using this in Azure logic app (inline code executor connector)
Anyone can help me with this?
You can see it matches the text in Atom:
Inside a string you need to escape the backslashes.
Otherwise the backslash will escape the next character. So, writing \b will escape the character b instead use \\b which will escape the \
var reg = new RegExp ('(\\bin region\\b)(?!.*\\1).+?(?=<)');

Single Backslash to double backslash Conversion [duplicate]

This question already has answers here:
JavaScript backslash (\) in variables is causing an error
(5 answers)
Escaping backslash in string - javascript
(5 answers)
Closed 3 years ago.
I want all single backslashes to be converted into double backslash
"C:\Users\MyName\ringtone.mp3" --> "C:\\Users\\MyName\\ringtone.mp3"
But for some reason it returns "C:UsersMyNameingtone.mp3"
So far I have tried the escape() function and the encodeURI() function but they don't work either. Partial of the string comes from nodejs OS Module which only returns with a single backslash on windows (homedir() function).
Here is what I have so far in the function
function normalize(path: string): string {
return path.normalize().replace(/\\/g, '\\');
}
Thanks in Advance
This should work:
var original = 'C:\\Users\\MyName\\ringtone.mp3';
var replaced = original.normalize().replace(/\\/g, '\\\\');
console.log('Original: ' + original);
console.log('Replaced: ' + replaced);
From what I see you had 2 problems:
First, it seems you were initializing your string like this:
var original = 'C:\Users\MyName\ringtone.mp3'
This would make your actual string value C:UsersMyNameingtone.mp3 because a \ character in javascript symbolizes an escape character.
Second, is because the \ character is an escape character, so the '\\' in your replace function is only looking to replace the matching pattern with a single backslash.

Switching \n with \\n in javascript [duplicate]

This question already has answers here:
How do I handle newlines in JSON?
(10 answers)
Closed 4 years ago.
So I have a string:
var s = "foo\nbar\nbob";
I want the string to become:
"foo\\nbar\\nbob"
How can I replace every \n with a \\n?
I've tried using some for loops, but I can't figure it out.
A simple .replace would work - search for \n, and replace with \\n:
var s = "foo\nbar\nbob";
console.log(
s.replace(/\n/g, '\\\n')
// ^^ double backslash needed to indicate single literal backslash
);
Note that this results in "a single backslash character, followed by a literal newline character" - there will not be two backslashes in a row in the actual string. It might be a bit less confusing to use String.raw, which will interpret every character in the template literal literally:
var s = "foo\nbar\nbob";
console.log(
s.replace(/\n/g, String.raw`\
`) // template literal contains one backslash, followed by one newline
);

How to replace all spaces present in a string with underscore using javascript? [duplicate]

This question already has answers here:
Replacing spaces with underscores in JavaScript?
(11 answers)
Closed 4 years ago.
I have a string with spaces separating words. I want to replace all the spaces in the string with underscore. Please tell me any small code for that because my solution is taking too much space.
Example : 'Divyanshu Singh Divyanshu Singh'
output : 'Divyanshu_singh_Divyanshu_Singh'
Try this one:
var str = "Divyanshu Singh Divyanshu Singh";
var res = str.replace(/ /g, "_");
console.log(res);
Use, / /g is a regex (regular expression). The flag g means global and causes all matches to be replaced.

Reg Ex validator not working in javascript [duplicate]

This question already has answers here:
Backslashes - Regular Expression - Javascript
(2 answers)
Closed 7 years ago.
regularExpression = '[\w]:\\.*';
function validate() {
debugger;
var regex = new RegExp(regularExpression);
var ctrl = document.getElementById('txtValue');
if (regex.test(ctrl.value)) {
return true;
} else {
return false;
}
}
Above is my code with which I am validating a directory path being entered. Here is the link for validator website which shows the perfect match for C:\ or D:\ or D:\Abcd\List.odt. But when I try to run the above JavaScript it fails.
When the Regular Expression string '[\w]:\\.*' is parsed by the JavaScript's RegEx engine, it will treat \w as an escaped character and since \w has no special meaning as an escaped character, it will treated as w only.
To fix this, you need to escape the \ like this
var regularExpression = '[\\w]:\\.*';
Or use RegEx literal like this
var regularExpression = /[\w]:\.*/;
As pointed out by Cyrbil in the comments, the character class [\w] is the same as \w only. So you can safely omit the square brackets around \w.

Categories