JS replace not working on string
.replace()
returns a new string (it does not modify the existing string) so you would need:
answer_form = answer_form.replace(/#/g, question_num);
You probably should also make question_num
a string though auto type conversions probably handle that for you.
Working example: http://jsfiddle.net/jfriend00/4cAz5/
FYI, in Javascript, strings are immutable - an existing string is never modified. So any method which makes a modification to the string (like concat
, replace
, slice
, substr
, substring
, toLowerCase
, toUpperCase
, etc...) ALWAYS returns a new string.
Javascript .replace() not working
Using .replace()
with a string will only fix the first occurrence which is what you are seeing. If you do it with a regular expression instead you can specify that it should be global (by specifying it with a g
afterwards) and thus take all occurrences.
carList = "<center>blabla</center> <b>some bold stuff</b> <b>some other bold stuff</b>";
alert(carList);
carList = carList.replace(/<center>/g,"").replace(/<\/center>/g,"").replace(/<b>/g,"").replace(/<\/b>/g,"");
alert(carList);
See this fiddle for a working sample.
.replace javascript not working
Strings in JavaScript are immutable. They cannot be modified.
The replace
method returns the modified string, it doesn't modify the original in place.
You need to capture its return value.
var data = 'http://baab.bh/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/t/e/test.jpg';data = data.replace('/image/', '/image/440x600/');console.log(data);
.replace in not working in js
You need to use .toString()
to cast data
to string. Once .toString()
applied you can call replace()
method.
Check this fiddle i created for easier understanding: http://jsfiddle.net/6sxf5d29
string.replace not working in node.js express server
msg = msg.replace(/%name%/gi, "myname");
You're passing a string instead of a regex to the first replace, and it doesn't match because the case is different. Even if it did match, you're not reassigning this modified value to msg
. This is strange, because you're doing everything correctly for tmp
.
`string.replace` doesn’t change the variable
According to the Javascript standard, String.replace
isn't supposed to modify the string itself. It just returns the modified string. You can refer to the Mozilla Developer Network documentation for more info.
You can always just set the string to the modified value:
variableABC = variableABC.replace('B', 'D')
Edit: The code given above is to only replace the first occurrence.
To replace all occurrences, you could do:
variableABC = variableABC.replace(/B/g, "D");
To replace all occurrences and ignore casing
variableABC = variableABC.replace(/B/gi, "D");
javascript string.replace not working
replace
returns a new string with the replacement, it doesn't change the string you call it on.
newbuffer = newbuffer.replace( matches[i], p.shortcodes[x].vala );
Does .replace take "plain text" or will I need to do some regex?
You can call replace
either with a string as the "search" argument, or with a regex. If you give it a string, it will replace the first occurrence of that string. If you give it a regex, it will replace the first match, or all matches, depending on whether you include the g
("global") flag on the regex.
Javascript string replace not working
The problem is the returned value of the replace method is not assigned:
description = description.replace("<?player>", req.session.player);
JS Fiddle: http://jsfiddle.net/LEBRK/
Related Topics
How to Prevent the Backspace Key from Navigating Back
What Is the Lifecycle of an Angularjs Controller
How to Return a Variable from Google Maps JavaScript Geocoder Callback
Convert Array of Strings into an Array of Objects
How to Search in Array of Object in Mongodb
Why Does the Promise Constructor Need an Executor
The Mystery of the Disappearing Checkmarks
How to Convert a String of Numbers to an Array of Numbers
Test If a Data Exist in Firebase
What Is the "Hasclass" Function with Plain JavaScript
What Does (Function (X,Y){...})(A,B); Mean in JavaScript
How to Call a C# Function from JavaScript
How to Access Constants in the Lib/Constants.Js File in Meteor
Smart Way to Truncate Long Strings