regex - JavaScript replace is not working if second argument is again a replace method -


i playing javascript string function , trying replace string below

actual string : 'microsoft'  replace :  '\'$\'' not defined 

i tried simple replace like

"microsoft".replace("microsoft","\'$\' not defined"); 

and results

' not defined. 

but want preserve $ sign thought write function preserve $ , return string; , same string second parameter 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('\'$\' not defined.');     var res = "microsoft".replace("microsoft",newstr);     document.getelementbyid("demo").innerhtml = res;     document.getelementbyid("test").innerhtml = newstr; } </script>  </body> </html> 

but not working, can me ?

$' speical token string.replace:

$' inserts portion of string follows matched substring.

you need use $$ insert literal $.

"microsoft".replace("microsoft","'$$' not defined"); 

see https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/string/replace


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

jsf - How to ajax update an item in the footer of a PrimeFaces dataTable? -

django - CSRF verification failed. Request aborted. CSRF cookie not set -