Set \ as string in JavaScript [duplicate] - javascript

This question already has answers here:
JavaScript backslash (\) in variables is causing an error
(5 answers)
Closed 7 years ago.
How do I set a \ as a string in JavaScript I am using it like this
Var x = {"\":"some value"}
But it's says that that is illegal, so then I tried it like '\' this but still the same error happened.

You can use escaping characters so add one more \ it will be escaped and become a string like this var x = {"\\":"some value"}

Just add an other slash '\' before it.

Related

Javascript regex that escapes \ sign [duplicate]

This question already has answers here:
Remove all backslashes in Javascript
(4 answers)
Closed 4 years ago.
I am trying to convert string that has following values
"A\"s\"sets"
my goal is to remove from string \ values no matter how many of them appear in string.
"A"s"sets"
I tried using new RegExp but I do not manage to perform that operation.
I even managed to create regex that will pick up everything except \ sign
[a-zA-Z0-9'"*]
I also tried calling on
regex.exec(string)
but I am getting an array instead of cleared string.
Anyone have any idea how to do this ?
Thank you
You can use replace.
let str = `"A\"s\\"sets"`
let op = str.replace(/\\+/g, '')
console.log(op)

Replace all in javascript [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 6 years ago.
How do I replace all occurences of "http://localhost" in a string in javascript?
I have res=res.replace(/^http\:\/\/localhost, url);, but it does not work. How do I fix it?
url is a variable and but "localhost" is a string.
UPDATE:
With the solutions below, I still get: ReferenceError: localhost is not defined. What am I missing?
UPDATE 2:
This is the (Perl) code that inserts the JS on the page:
$form .= qq|<script>res='$doc'; loc=document.location.href; url=loc.substring(0,loc.indexOf(":8080")); res=res.replace(/http\:\/\/localhost/g, url); document.location='data:text/html;charset:utf-8,' + res; </script>|;
use /g to replace every occurrence in your string
Something like this
str = str.replace(/http:\/\/localhost/g,'');
res=res.replace(/http\:\/\/localhost/g, url);
'g' indicates to replace all.

Need regex to replace entire folderpath while preserving file name where last folder could by anything [duplicate]

This question already has answers here:
How to get the file name from a full path using JavaScript?
(21 answers)
Closed 6 years ago.
I need a regex that would replace something like this but leave the file name.
/folder1/folder2/folder3/anything/somefile.html
Also could someone show me how to implement this with replace method? Replacing the entire path match to empty string and again leaving the file and which would be anything.
Thanks in advance.
You can do it without regular expressions:
var filename = string.split('/').pop();
// "somefile.html"
You can use .*\/.
. will match anything
* will repeat the previous zero or more times.
\/ is a literal slash (/). But needs to be escaped because it's part of the regex construct:
var str = '/folder1/folder2/folder3/anything/somefile.html';
str.replace(/.*\//, ''); // "somefile.html"

Javascript character replace all [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 7 years ago.
I am trying to find all the characters ('?') of a URL and replace it with &.
For instance, i have var test = "http://www.example.com/page1?hello?testing";
I first attempted:
document.write(test.replace("&","?"))
This resulted in that only the first ? would be replaced by & , then I found a question saying that I could add a g(for global)
document.write(test.replace("&"g,"?"))
Unfortunately, this did not have any effect either.
So how do I replace all characters of type &?
You need to escape the ? character like so:
test.replace(/\?/g,"&")
You're almost there.
the thing you saw in SO is regex replace :
document.write(test.replace(/\?/g,"&")) ( I thought you wanted to change & to ? , but you want the opposite.)
with the G flag - it will replace all the matches in the string
without it - it will replace only the first match.

new Regexp doesn't work [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 4 years ago.
I'm trying to convert following expression to "new Regexp()" style:
http://jsfiddle.net/HDWBZ/
var Wyrazenie = /\btest[a-z]*/g;
I'm really confused with it and have no idea how to fix it. Below is what I've done but obviously it doesn't work.
var Wyraznie = new RegExp("\btest[a-z]*","g");
Also have a question how would it look if instead of "test" I would use variable?
You should use this instead...
new RegExp("\\btest[a-z]*", "g");
... as \b will be interpolated into a single (slashless) character when JavaScript parser works through the corresponding string literal. The solution is to escape slash itself.
DEMO: http://jsfiddle.net/HDWBZ/1/

Categories

Resources