I am playing with JavaScript string function and was trying to replace a string like below
Actual String : 'Microsoft'
Replace with : '\'$\'' is not defined
I tried a simple replace like
"Microsoft".replace("Microsoft","\'$\' is not defined");
and it results
' is not defined.
But I want to preserve $ sign so thought to write another function which will preserve the $ and return the string; and the same string would be the second parameter for the actual replace method.
<!DOCTYPE html>
<html>
<body>
<p id="demo">Microsoft</p>
<p id="test"></p>
<button onclick="myFunction()">Try it</button>
<script>
function stripslashes(str) {
str=str.replace("/\\'/g",'');
return str;
}
function myFunction() {
var newStr = stripslashes('\'$\' is not defined.');
var res = "Microsoft".replace("Microsoft",newStr);
document.getElementById("demo").innerHTML = res;
document.getElementById("test").innerHTML = newStr;
}
</script>
</body>
</html>
But it is not working, can anyone help me with it ?
\in the original string are being parsed away by JS when the assignment statement is executed.