0

I want to replace string data with another string data.

I have tried to do so with the following code.

$('#btnchkProduct').click(function () {
    var  stringfirst="hi this is first data";
    var  stringsecound="this is another data";

    var finaldata = string.replace(stringfirst , stringsecound );
    alert(finaldata );
});
2
  • remember to use /g to replace all , ex: mystring.replace(/oldstring/g,'newstring') Commented Jan 4, 2013 at 12:59
  • Where does the news variable come from? And the string variable? Commented Jan 4, 2013 at 13:04

3 Answers 3

1
$('#btnchkProduct').click(function() {
  var stringfirst = "hi this is first data";
  var stringsecound = "this is another data";

  var finaldata = stringfirst.replace(stringfirst , stringsecound );
  alert(finaldata);
});​
Sign up to request clarification or add additional context in comments.

Comments

1

Jquery is just a wrapper to javascript . String replacement functions are there in javascript core , you can use that directly

var str="Visit Microsoft!";
var n=str.replace("Microsoft","W3Schools");

Refer Reference

Comments

0

You can Simply use

$('#btnchkProduct').click(function () {
    var  stringfirst="hi this is first data";
    var  stringsecound="this is another data";

    var finaldata = stringsecound.replace(stringsecound, stringfirst);
    alert(finaldata );
});

the second string will be replaced by first string

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.