I have this:
var $string = 'hello,world';
$(".content").text(function(i, $string) {
return $string.replace(/,/g, ", ");
});
Aiming to add ma after space. What am I doing wrong? The problem is likely how I am using the variable in the function. Entry level stuff.
I have this:
var $string = 'hello,world';
$(".content").text(function(i, $string) {
return $string.replace(/,/g, ", ");
});
Aiming to add ma after space. What am I doing wrong? The problem is likely how I am using the variable in the function. Entry level stuff.
Share Improve this question asked Jan 26, 2018 at 14:24 Henrik PettersonHenrik Petterson 7,10421 gold badges80 silver badges157 bronze badges 8-
2
Are you trying to change the
$string
variable or the text of the.content
element? Your code seems to indicate both, but only the latter will work. – Rory McCrossan Commented Jan 26, 2018 at 14:27 - 1 Possible duplicate of Regex to add a space after each ma in Javascript – urbz Commented Jan 26, 2018 at 14:27
-
$(".content").text($string.replace(/,/g, ", "));
is working fine. – Striped Commented Jan 26, 2018 at 14:29 -
@RoryMcCrossan I am trying to change the
$string
variable by adding space after ma and then insert it into.content
. You mentioned in an answer that you had a solution to this without creating a function? – Henrik Petterson Commented Jan 26, 2018 at 14:30 -
1
See my ment on @RenzoCC's answer. Also note that
$string
shouldn't have the$
prefix as it holds a string, not a jQuery object. – Rory McCrossan Commented Jan 26, 2018 at 14:30
3 Answers
Reset to default 3Don't include arguments in your anonymous function since this is overwriting your $string value... As suggested by @RoryMcCrossan you don't need a function
var $string = 'hello,world';
$(".content").text($string.replace(/,/g, ", "));
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="content"></div>
For your understanding, your code is currently working like this one :
var $anotherstring = 'hello,world';
$(".content").text(function(i, $string) {
return $string.replace(/,/g, ", ");
});
(because the inner variable parameter of function is not the same as the first one, and hide the first $string
you defined with the same name)
The way you are using text
with the inner function(i, $string)
will work if the current text value of the element is 'hello,world'
(see the jquery documentation)
$(".content").text('hello,world'); // just to set the value of the .content element for the example, you can ignore this if this already the case
$(".content").text(function(i, $string) {
return $string.replace(/,/g, ", ");
});
So the above code will work if you want to transform the existing content.
If you simply want to use the value of another variable, and not the old content of the .content element, you can simply do :
var $string = 'hello,world';
$(".content").text($string.replace(/,/g, ", "))
Try this way
let $str = 'hello,world';
$(".content").text($str.replace(/,[s]*/g, ", "));
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="content"></div>