0

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 ?

3
  • Umm, what are you actually trying to achieve? Commented Sep 9, 2014 at 20:19
  • Your \ in the original string are being parsed away by JS when the assignment statement is executed. Commented Sep 9, 2014 at 20:19
  • "Microsoft".replace(/(Microsoft)/,"\'$1\' is not defined"); Commented Sep 9, 2014 at 20:34

2 Answers 2

1

$' is a speical token for String.replace:

$' Inserts the portion of the string that follows the matched substring.

You need to use $$ to insert a literal $.

"Microsoft".replace("Microsoft","'$$' is not defined");

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

Sign up to request clarification or add additional context in comments.

2 Comments

Also: "\'$\' is not defined" is the same as "'$' is not defined", and the slashes do nothing
this doesn't create the string he wants '\'$\'' is not defined
0
"Microsoft".replace("Microsoft","\\'$\\' is not defined");

5 Comments

I'm sure he doesn't want to have any backslashes present in his result.
look at his question: Actual String : 'Microsoft' Replace with : '\'$\'' is not defined .. the answer merits a downvote if its wrong, this gives him what he asked for
I don't know, we'd need a comment from the OP for on that. However, he explicitly tries to use a stripSlashes() function… None of the downvotes is from me, sorry.
no worries @Bergi :) i totally agree with you .. we need to hear back from the OP
My actual problem is, '\'$\' is not defined' is a result of parser and that parser does one more thing like '{content}'.replace('{content}','\'\$\'is not defined'); When it tries to replace it replaces " ' " only as $ has special meaning in replace. So my entire thought process was, '{content}'.replace('{content}', '\'$\' is not defined'.replace('\'$\'','$') );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.