I’m using a method to iteratively perform a replace in a string.
function replaceAll(srcString, target, newContent){
while (srcString.indexOf(target) != -1)
srcString = srcString.replace(target,newContent);
return srcString;
}
But it doesn’t work for the target text that I want, mainly because I can’t think of how to properly write that text: What I want to remove is, literally, "\n", (included the comma and the quotes), so what to pass as second param in order to make it work properly?
Thanks in advance.
You need to escape the quotes, if you use double quotes for the first argument to
replace'some text "\n", more text'.replace("\"\n\",", 'new content');or you can do
'some text "\n", more text'.replace('"\n",', 'new content');Note in the second example, the first argument to replace uses single quotes to denote the string, so you don’t need to escape the double quotes.
Finally, one more option is to use a regex in the
replaceinvocation'some text "\n", more text "\n",'.replace(/"\n",/g, 'new content');the “g” on the end makes the replace a replace-all (global).