Replace all in javascript [duplicate] - javascript

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.

Related

Replace String with value `$&' not working in javascript [duplicate]

This question already has answers here:
Why 'ABC'.replace('B', '$`') gives AAC
(2 answers)
Closed 1 year ago.
I have string show {{value}} and I want replace {{value}} with $& but it not work. It return current value show {{value}}.
Here is my code
let data ="show {{value}}";
let output = data.replace("{{value}}","$&");
alert(output);
I don't know why it not work. I try replace with other strings same $1, $a and it work.
How I can fix my problem
$ is a special symbol in javascript. Write $$& instead and it should work :)

Replace quote sign in JavaScipt [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 2 years ago.
I got following code snippet, it's pretty simple,
var b = "'aa','bb'";
console.log(b.replace("'", ""));
// result is "aa','bb'"
I want replace all single quote signs with blank. So my expected output should be "aa,bb", but the actual output is "aa','bb'" neither run this code snippet in Node nor browser. Seems only the first single quote sign has been replaced.
I already got a workaround to resolve this problem by replace with regex. What I wanna know
is what happened to replace function here? I cannot figure this out.
Try using RegEx specifying the global flag (g) that matches the pattern multiple times. Please also note that, as replace() does not modify the original string you have to reassign the modified value to the variable:
var b = "'aa','bb'";
b = b.replace(/'/g, "");
console.log(b);

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