.replace doesn't work [duplicate] - javascript

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 4 years ago.
I wrote this code to replace some chars in a string:
$(".rtl:not(.num)").keypress(function(e)
{ var key = (e.keyCode || e.which);
var vlu = $(this).val();
var charTyped = String.fromCharCode(key);
if (charTyped=='ك')
{ vlu.replace(/ك/g,'ک');
alert("keh"); }
if (charTyped=='ي')
{ vlu.replace(/ي/g,'ی');
alert("yeh"); }
alert(vlu);
});
After the code executes, vlu has not changed. What is wrong?

Replace does not change the original string, it returns a new string.
MDN String replace()
var str = "abc123";
var updated = str.replace("123","");
console.log("str: ", str);
console.log("updated: ", updated);

Related

How to replace the second occurrence of a string in javascript [duplicate]

This question already has answers here:
Simple Javascript Replace not working [duplicate]
(3 answers)
Closed 5 years ago.
I am trying to replace the second occurrence of a string in javascript. I'm using a regex to detect all the matches of the character that I'm looking for. The alert returns the same initial text.
text = 'BLABLA';
//var count = (texte.match(/B/g) || []).length;
var t=0;
texte.replace(/B/g, function (match) {
t++;
return (t === 2) ? "Z" : match;
});
alert(text);
https://js.do/code/157264
It's because you never use the result returned by the replace function.
Here's the corrected code:
const text = 'BLABLA'
let t = 0
const result = text.replace(/B/g, match => ++t === 2 ? 'Z' : match)
console.log(result)

How to cut dot in string? [duplicate]

This question already has answers here:
Javascript string replace not working [duplicate]
(3 answers)
Closed 6 years ago.
I tried to cut dot from string using the following function:
function removeSymbols(str){
console.log(str.length);
str.replace(/\./g, "");
return str;
}
var str = " народу.";
But it does not cut
Change your return statement from
return str;
To
return str.replace(/\./g, "");
function removeSymbols(str) {
console.log(str.length);
str = str.replace(/\./g, "");
return str;
}
var str = " народу.";
console.log(removeSymbols(str));
replace doesn't change the original string, it will return a new string that is replaced.

How to increment only digits of string? [duplicate]

This question already has answers here:
How to increment number in string using Javascript or Jquery
(6 answers)
Closed 6 years ago.
I may have following type strings
A1 or 1A or AB....1 or 1AB......
so how to increment only digits of above type of strings in javascript?
var adminNo = data.Admission_No.slice(-2);
alert(adminNo);
var removedNo = data.Admission_No.substring(data.Admission_No.length-1);
alert(removedNo);
Use the replace method as shown in demo below
function incrementer(input)
{
return input.replace(/\d+/, function(match){ return parseInt(match) + 1 });
}
alert(incrementer("A1"));
alert(incrementer("1A"));
This will find the integer anywhere in the input string and increment it by one.
string.replace(/\d+/, function(n){ return ++n });
You can do it by taking out integer from your string
Its big long, but more self-explainatory
var youroriginalstring="A1"
var withNoDigits = youroriginalstring.replace(/[0-9]/g, '');
var yournumber = youroriginalstring.replace ( /[^\d.]/g, '' );
var incNos=yournumber +1;
var newString = incnos + "withNoDigits"

Get the same function for replace [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 8 years ago.
I have two strings like these
var temp = 'xx-y1 xx-y2 xx-y3';
var temp1 = 'zz-y1 zz-y2 zz-y3';
I wanna replace all the words started with "xx-" and "zz-" pattern and for this purpose I do this.
temp.replace(/\bxx-\S+/g, '');
temp.replace(/\bzz-\S+/g, '');
now my question is how can I have a single function and just call it?
I try to test this but it doesn't work!!!
func = function(str, pattern) {
return str.replace(RegExp('\b' + pattern + '\S+', 'g'), '');
}
You need to escape \ when calling RegExp constructor.
function replace(where, what) {
return where.replace(new RegExp('\\b' + what + '\\S+', 'g'), '');
}

Replacing %1 and %2 in my javascript string [duplicate]

This question already has answers here:
JavaScript equivalent to printf/String.Format
(59 answers)
Javascript multiple replace [duplicate]
Closed 9 years ago.
Lets say I have the following string in my javascript code:
var myText = 'Hello %1. How are you %2?';
Now I would like to inject something in place of %1 and %2 in the above string. I can do:
var result = myText.replace('%1', 'John').replace('%2', 'today');
I wonder if there is a better way of doing than calling 2 times the replace function.
Thanks.
How about a little format helper? That's basically what you need:
function format(str, arr) {
return str.replace(/%(\d+)/g, function(_,m) {
return arr[--m];
});
}
var myText = 'Hello %1. How are you %2?';
var values = ['John','today'];
var result = format(myText, values);
console.log(result); //=> "Hello John. How are you today?"
Demo: http://jsbin.com/uzowuw/1/edit
Try this sample
function setCharAt(str,chr,rep) {
var index = -1;
index= str.indexOf(chr);
var len= chr.length;
if(index > str.length-1) return str;
return str.substr(0,index) + rep + str.substr(index+len);
}
var myText = 'Hello %1. How are you %2?';
var result = setCharAt(myText,"%1","John");
var result = setCharAt(result,"%2","today");
alert(result);
This is meant just as a complex comment to elclarns' great answer, suggesting these alternatives:
Can be written as String.prototype
Can use arguments
The function can be altered to
String.prototype.format = function() {
var args=arguments;
return this.replace(/%(\d+)/g, function(_,m) {
return args[--m];
});
}
And called this way
var result = "I am %1, %2 years old %1".format("Jan",32);
// I am Jan, 32 years old Jan

Categories

Resources