Trim Results

I have a function that returns values ​​from a database. My problem: the function adds "" to the returned values. I need to trim the values ​​before displaying them on the user screen.

This is my function

 <script language="JavaScript" type="text/javascript">
    function handleProcedureChange(procedureid)
    {
        procedureid= document.form1.procedure.value;
        //alert(procedureid);
        var url ="some URL goes here"; 
        url=url+"ProcedureID="+procedureid;

        $.get(url, function(procedureResult) {
            $("#procedureDescription").text(procedureResult);
        });
    }
   </script>

prcedureResult is the return value and the value that I need to trim from the quotes before displaying it.

+3
source share
2 answers

Use this to remove quotes:

$.get(url, function(procedureResult) {
     procedureResult = procedureResult.replace(/^"+|"+$/g, "");
     $("#procedureDescription").text(procedureResult);
});

This replaces "from the beginning ( ^) and ends ( $) the string with nothing ( "")

+3
source

Try the following function, which will remove all leading and trailing quotes from the string value

function stripQuotes(str) {
  str = str.replace(/^\"*/, '');
  str = str.replace(/\"*$/, '');
  return str;
}

It can be used as

$.get(url, function(procedureResult) {
  procedureResult = stripQuotes(procedureResult);
  $("#procedureDescription").text(procedureResult);
});
+3
source

All Articles