Simple Javascript Replace not working [duplicate] - javascript

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 3 years ago.
This seems so simple and trivial but it is not working. Here is my javascript:
var url = "/computers/";
console.log(url);
url.replace(/\//gi, " ");
console.log(url);
And here is the output in my browsers console:
/computers/
/computers/
As you can see nothing changes. As you can tell from the code I'm trying to replace the forward slashes with spaces. What am I doing wrong?

url = url.replace(/\//gi, " ");

Nothing changes because you're not assigning the result of the replacement to a variable. Add url = url.replace()

url.replace(/\//gi, " "); returns the resulting string (in javascript you can't modify an existing string), you are not assigning it to anything
assign it like so:
url = url.replace(/\//gi, " ");

Related

I had an error when I passing a string argument to a function? [duplicate]

This question already has answers here:
How can I pass a parameter to a setTimeout() callback?
(29 answers)
Closed 4 years ago.
I'm sorry. I don't know how to describe this problem carefully.so I hope put my code first.
var test = 'less';
function test_I(tite) {
console.log(tite);
}
var repeat = "test_I('"+test+"')";
setTimeout(repeat,10);
The code is right, but when I change:
var repeat = "test_I(' "+test+" ')";
to:
var repeat = "test_I("+test+")";,
I get this error in Chrom's console. Why do I need a ' ' When I pass a
String parameter? The error is below.
Why I need a ' ' When I pass a String parameter
To ensure that less is passed as a String instead of a variable.
If you don't use '', this code var repeat = "test_I("+test+")"; becomes
var repeat = "test_I(less)";
and when setTimeout executes the same it will look for variable less.
Ah, yes ... interesting fun. If you break down your string to what the JavaScript engine will see, it becomes:
test_I(less)
and less doesn't exist.
if you added "var less='less'" at the beginning of your code, it would then work.

Get the query string value from a URL using javascript [duplicate]

This question already has answers here:
How can I get query string values in JavaScript?
(73 answers)
Closed 6 years ago.
I have a URL like
http://something.com?acess_token=ashv6786sdjfhjd
When I click on that link, how to get the access_token from that link using Javascript?
Try :
var Yoururl= "http://something.com?acess_token=ashv6786sdjfhjd";
var acessToken=Yoururl.split('?')[1].split('=')[1];
Working Fiddle
For url parameter see old so question from given link.
Get url parameter via jquery
If you want the value of the access_token you mean this:
ashv6786sdjfhjd
Then here's a quick solution. Hope it helps!
var str = "http://something.com?acess_token=ashv6786sdjfhjd.when";
var accessToken = str.split("=")[1].split(".")[0];
console.log(accessToken);

Javascript unescape doesn't seem to work [duplicate]

This question already has answers here:
Unescape apostrophe (') in JavaScript?
(2 answers)
Closed 8 years ago.
I have a simple string which is
Company's
Now I have some javascript which is ran when a form is submitted
var jsCompanyName = '#Model.Name';
var unescapedCompanyName = unescape(jsCompanyName);
$('.selector-input').val(unescapedCompanyName);
$('.selector-input-id').val('#Model.Id');
Going thought with a debugger, my var unescapedCompanyName is still "Company's" even after the unescape function, does anyone have any idea on why this isn't removing ' and replacing it with a '
The unescape() function has nothing to do with HTML syntax. It's for handling escapes in URL syntax, which is a completely different thing. (It's also deprecated even for its intended purpose.)
There's no built-in function to deal with HTML escapes. However, code running in a web browser can do something like this:
function html_unescape(s) {
var div = document.createElement("div");
div.innerHTML = s;
return div.textContent || div.innerText; // IE is different
}
You can do this easily with JQUERY if you really need to:
function htmlDecode(value) {
return $('<div/>').html(value).text();
}
var str = 'Company's';
console.log(htmlDecode(str)); // Company's
JSFIDDLE.

Javascript replace not working with ampersand and defining a letter [duplicate]

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 8 years ago.
I have written a JSFiddle with the expected output and output my code is currently doing. The two different values must be parsed as either a colon or a semi-colon as I need to know what one line to parse in php is.
var data = "key=update.repositories&value=xime+mcsg+mcsg-maps&key=server.minPlayersToStart&value=12";
data.replace(/&v/g, ":v");
data.replace(/&k/g, ";k");
$(".encData").text(data);
Fiddle found here: http://jsfiddle.net/RS6xC/1/
string.replace() doesn't change the original variable, it returns a new value.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
So you need to reassign the returned value of the replace() method to the original variable, such as like this:
var data = "key=update.repositories&value=xime+mcsg+mcsg-maps&key=server.minPlayersToStart&value=12";
data = data.replace(/&v/g, ":v");
data = data.replace(/&k/g, ";k");
$(".encData").text(data);
.replace returns a new string, it does not modify the existing string.
Use:
data=data.replace(/&v/g, ":v");
data=data.replace(/&k/g, ";k");

Replace function not replacing [duplicate]

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 8 years ago.
I followed some documentation to use the JavaScript replace function and it's not changing anything. No errors are thrown. Any idea what I'm doing wrong? The variable is retrieved from XML - maybe it needs to be cast as a string or something?
for (var i = 0, iln = projects.length; i < iln; i++){
var thumb = projects[i].get('thumb');
thumb.replace("200.jpg", "640.jpg");
console.log(thumb) //200.jpg not replaced
}
The full thumb value should look like this:
http://b.vimeocdn.com/ts/160/895/160895498_200.jpg
Is there a better way to find and replace things?
Assign the value back into thumb.
thumb = thumb.replace("200.jpg", "640.jpg");
Try:
thumb = thumb.replace("200.jpg", "640.jpg");

Categories

Resources