How to remove quotation marks (double quotes) from string using jQuery or Javascript?


I am working on a web-application which returns JSON results as "/FileURL", I just need to get /FileURL as a result, means I need to remove quotation marks (double quotes) from returned JSON string using jQuery or Javascript, how can I do that?


Asked by:- Vinnu
3
: 35901 At:- 5/29/2018 3:05:27 PM
Javascript jQuery remove double quotes







4 Answers
profileImage Answered by:- neena

You can try using regex to remove quotes from your string

var strWithOutQuotes= strWithQuotes.replace(/['"]+/g, '')
  • ['"] is a character class, matches both single and double quotes. you can replace this with "to only match double quotes.
  • +: one or more quotes, chars, as defined by the preceding char-class (optional)
  • g: the global flag. This tells JS to apply the regex to the entire string. If you omit this, you'll only replace a single char.

Above solution will remove double quotes from your string.

If you just want to remove double quotes from start and end of you string you can try

var strWithOutQuotes= strWithQuotes.replace(/^"(.*)"$/, '$1');

Another way, possible the simplest example

var strWithOutQuotes= strWithQuotes.replace(/"/g, '');

here is the working fiddle of the last solution.

3
At:- 5/30/2018 2:48:43 PM Updated at:- 7/26/2022 2:50:22 PM
Thanks for your answer, It works as required. 0
By : Vinnu - at :- 6/4/2018 7:33:27 AM


profileImage Answered by:- vikas_jk

You can also simply use to remove all double quotes (")

str = str.replace(/"/g,"");

OR

If you want to remove only quotes around the string, you can use:

var str = '"Your "quoted" string"';
console.log( clean = str.replace(/^"|"$/g, '') );
//Your "quoted" string

OR

You can create custom JS function as shown below

function RemoveDoubleQuotes(a) {
    var fIndex = a.indexOf('""');
    var lIndex = a.lastIndexOf('""');
    if(fIndex >= 0 && lIndex >= 0){
      a = a.substring(fIndex+1, lIndex+1);
    }
    return a;
    
}

console.log(RemoveDoubleQuotes('"Testing"'));  // Output->  Testing
console.log(RemoveDoubleQuotes('""test""'));  // output -> test
3
At:- 9/15/2020 11:30:22 AM Updated at:- 7/26/2022 2:50:22 PM


profileImage Answered by:- Sam

If you want to remove the double quotes from JSON returned data, you can simply remove it using JSON.Parse method like below

JSON.parse(Double_quoted_string);

JSON.parse("My test");  // for example

You can also do it using eval

var No_Quotes_string= eval(Quoted_string);
2
At:- 6/4/2018 11:48:48 AM


profileImage Answered by:- bhanu

If string has double quotes in start/end of the string, you can simple use .slice function which is easier to use than regex

str = str.slice(1, -1);

This is quick work, so work for limited strings only.

2
At:- 4/19/2022 3:47:59 PM
thanks for easiest answer. 0
By : Vinnu - at :- 5/27/2022 7:52:53 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use