Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Help Need in replace function in java script

mkmuralymkmuraly Member Posts: 6
Hi There,

In one of my applications i need to replace some value from an incoming string to another value. Basicaly im passing in a xml string which has the xml header (<?xml version="1.0"?>), i need a script to which finds the <?xml version="1.0"?> and replace it using <?xml version="1.0" encoding="UTF-8"?>

I have written a script for this but not sure how ensure that the special characters in the search string also treated as part of the search string.

Sample Source message

<?xml version="1.0"?>xxxxyyyzzz

The result should be

<?xml version="1.0" encoding="UTF-8"?>xxxxyyyzzz

here is script

Function ReplaceHeader(bodystr)
{
var str="<?xml version="1.0"?>";
var newstr="<?xml version="1.0" encoding="UTF-8"?>";

var newbody="";

bodystr.replace(/<?xml version="1.0"?>/i, "<?xml version="1.0" encoding="UTF-8"?>");

newbody=bodystr;

return newbody;
}

regards
Muraly

Comments

  • ManiFEstManiFEst Member Posts: 3
    [b][red]This message was edited by ManiFEst at 2005-9-13 4:12:3[/red][/b][hr]
    : Hi There,
    :
    : In one of my applications i need to replace some value from an incoming string to another value. Basicaly im passing in a xml string which has the xml header (<?xml version="1.0"?>), i need a script to which finds the <?xml version="1.0"?> and replace it using <?xml version="1.0" encoding="UTF-8"?>
    :
    : I have written a script for this but not sure how ensure that the special characters in the search string also treated as part of the search string.
    :
    : Sample Source message
    :
    : <?xml version="1.0"?>xxxxyyyzzz
    :
    : The result should be
    :
    : <?xml version="1.0" encoding="UTF-8"?>xxxxyyyzzz
    :
    : here is script
    :
    : Function ReplaceHeader(bodystr)
    : {
    : var str="<?xml version="1.0"?>";
    : var newstr="<?xml version="1.0" encoding="UTF-8"?>";
    :
    : var newbody="";
    :
    : bodystr.replace(/<?xml version="1.0"?>/i, "<?xml version="1.0" encoding="UTF-8"?>");
    :
    : newbody=bodystr;
    :
    : return newbody;
    : }
    :
    : regards
    : Muraly
    :
    i think you are not familiar with RegExp syntax.
    newbody=bodystr.replace(/<?xml version="1.0"?>/i, "<?xml version="1.0" encoding="UTF-8"?>");
    and remove the next row "newbody=bodystr;"



  • mkmuralymkmuraly Member Posts: 6
    : [b][red]This message was edited by ManiFEst at 2005-9-13 4:12:3[/red][/b][hr]
    : : Hi There,
    : :
    : : In one of my applications i need to replace some value from an incoming string to another value. Basicaly im passing in a xml string which has the xml header (<?xml version="1.0"?>), i need a script to which finds the <?xml version="1.0"?> and replace it using <?xml version="1.0" encoding="UTF-8"?>
    : :
    : : I have written a script for this but not sure how ensure that the special characters in the search string also treated as part of the search string.
    : :
    : : Sample Source message
    : :
    : : <?xml version="1.0"?>xxxxyyyzzz
    : :
    : : The result should be
    : :
    : : <?xml version="1.0" encoding="UTF-8"?>xxxxyyyzzz
    : :
    : : here is script
    : :
    : : Function ReplaceHeader(bodystr)
    : : {
    : : var str="<?xml version="1.0"?>";
    : : var newstr="<?xml version="1.0" encoding="UTF-8"?>";
    : :
    : : var newbody="";
    : :
    : : bodystr.replace(/<?xml version="1.0"?>/i, "<?xml version="1.0" encoding="UTF-8"?>");
    : :
    : : newbody=bodystr;
    : :
    : : return newbody;
    : : }
    : :
    : : regards
    : : Muraly
    : :
    : i think you are not familiar with RegExp syntax.
    : newbody=bodystr.replace(/<?xml version="1.0"?>/i, "<?xml version="1.0" encoding="UTF-8"?>");
    : and remove the next row "newbody=bodystr;"
    :
    :
    :
    :
    Hi ManiFest,

    Thank you for the suggestion will try it out your solution.

    Have a nice day

    regards
    Muraly
Sign In or Register to comment.