0

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

3

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:

http://jsfiddle.net/rV8Cb/

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.

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

Comments

0

You need to define total_res in quotes. then it works fine for me.

var total_res = "R4-4R4-5";

http://jsfiddle.net/PxJrn/

Comments

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.