The question is published on by Tutorial Guruji team.
r = "Inamnhere"; s = r.replace("n"," ");
But instead of giving me
I am here
as the value of
It returns the same.
Where’s the problem??
As stated by the others the global flag is missing for your regular expression. The correct expression should be some thing like what the others gave you.
var r = "Inamnhere"; var s = r.replace(/n/g,' ');
I would like to point out the difference from what was going on from the start. you were using the following statements
var r = "Inamnhere"; var s = r.replace("n"," ");
For instance the method you made could be made more general to parse input from several different types of files. Due to differences in Operating system it is quite common to have files with n or r where a new line is required. To be able to handle both your code could be rewritten using some features of regular expressions.
var r = "Iramnhere"; var s = r.replace(/[nr]/g,' ');