How to remove backslah-quote from a JSON response

208
March 14, 2018, at 8:59 PM

I am getting a value returned from an ajax call that looks like \"3/7/2018\". I need to set the value of a jQuery datepicker using this returned value but as is it will set the date to 10/04/2023.

I tried to get rid of backslash-quote using the following but it ignores it and returns the original string. What am I doing wrong here?

var uNewDate = newDate.replace(/\\\"/gi, '');

here is the entire function:

function pageLoad() {
    setStartDate(new Date());
}
// \"3/7/2018\"
function setStartDate(newDate) {debugger
    $('#tbStartDate').unbind();
    $("#tbStartDate").datepicker({
        showOtherMonths: true,
        selectOtherMonths: true,
        showOn: "button",
        defaultDate: new Date(),
        buttonImage: "../assets/images/calendar.gif",
        buttonImageOnly: true,
        buttonText: "Select Start Date",
        option: "mm/dd/yy"
    });
    //var uNewDate = newDate.replace(/\\\"/gi, '');
    $("#tbStartDate").datepicker("setDate", newDate);
}
$.ajax({
    type: "POST",
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    url: "../services/easg.asmx/GetCertItemStartDate",
    cache: false,
    data: certItemID,
}).done(function (result) {debugger
    setStartDate(result.d);
}).fail(function (jqXHR, textStatus, errorThrown) {debugger
    alert(textStatus + ' - ' + errorThrown);
});
Answer 1

Remember you are handling JSON data as your response. Special characters are escaped in JSON, like your initial example is showing.

You need to parse it before handling it.

JSON.parse("\"3/7/2018\"")
// Result: "3/7/2018"
Answer 2

This isn't using RegEx, but this will work:

var testDateString = '\"3/7/2018\"'; 
 
var newDateString = testDateString.split('\"') 
  .join(''); 
 
console.log(newDateString);

Answer 3

console.log('\"3/7/2018\"'.replace(/[\\"]/g,''))

Rent Charter Buses Company
READ ALSO
Execute two ajax posts in one click and show content in two different places

Execute two ajax posts in one click and show content in two different places

I have a shopping cart from which I can remove products, and a cartpage with all products on itI can remove products from both seperately using ajax, but

137
how to load javascript professionally [on hold]

how to load javascript professionally [on hold]

There are too many java scripts in one page and whenever i load scripts other scripts wont workhow can i make it work with more cleaner and readable so that i could spot easily the errors

167
masonry-jquery ajax not able to get data on scroll

masonry-jquery ajax not able to get data on scroll

I trying to get more data as I scroll down the page, however the code runs well when I perform the click functionAfter the page has reached the bottom of the page I'm not able to get the data even if i get I'm getting dublicate rows from the database

181