So I have this string var total_res = R4-4R4-5; and I want to replace R4-5 substring with "",but I have tried this code so far and it doesn't work:
Javascript:
<script>
$(document).ready(function(){
ss = "R4-5";
lool = total_res.replace(ss,"");
alert(lool);//it alerts the same original string
});
</script>
What is wrong? Thank you for your help.
2 Answers 2
I'm not sure if you meant to or not, but total_res should be declared as a string...like:
var total_res = "R4-4R4-5";
Using your code, it works like that:
If you checked your browser's (error) console, you'd see: Unexpected token ILLEGAL.
Note that .replace() only replaces the first occurrence of the string. You'd have to use a regular expression if you want to replace every occurrence of "R4-5" in the string.
Comments
You need to define total_res in quotes. then it works fine for me.
var total_res = "R4-4R4-5";