1

I have a string that outputs like this:

"firstName" "lastName"

I need to the output to be like

"firtName lastName"

or else I get bad string request. I have tried different regex combinations that could achieve that however so far I can remove the quotes at the beginning and end of the string and the ones in the middle.

var string = '"firstName" ,"lastName"';
var stringWithoutCommas = string.replace(/,/g, '');
var stringWithoutExtQuotes = stringWithoutCommas.replace(/^"(.+(?="$))"$/, '$1');
console.log(stringWithoutExtQuotes); //firstName" "lastName

thanks you.

Fighithebird
  • 37
  • 1
  • 6
  • I have a strong feeling that this is an [XY problem](http://xyproblem.info/). It would probably be much better to fix the bad string request properly instead of removing the quotes manually. – Guy Incognito Dec 11 '20 at 19:30
  • @GuyIncognito I have another stackoverflow question here pertaining to this issue. https://stackoverflow.com/questions/65254872/polymer-reset-parameters-format-not-working unfortunately not a lot of folks know this framework. – Fighithebird Dec 11 '20 at 19:52
  • yyeah ok I'll add a comment to the other question. Doesn't look like this is an issue about quotes at all. – Guy Incognito Dec 11 '20 at 19:57

4 Answers4

1

var string = '"firstName" ,"lastName"';

console.log(
  string.replace(/"(\w*)\W*(\w*)"/, function(_, match1, match2){
    return `"${match1} ${match2}"`;
  })
);

You could grab the two strings and rebuild the string you want.

Taplar
  • 24,788
  • 4
  • 22
  • 35
1

One approach

const input = '"firstName" "lastName"'

console.log('"'+input.replace(/"(.+?)"/g,'$1')+'"')
uke
  • 893
  • 4
  • 10
1

Extract all between double quotes and join the results:

const string = '"firstName" ,"lastName"';
console.log(Array.from(string.matchAll(/"([^"]+)"/g), x=>x[1]).join(" "));

Regex: "([^"]+)"

Explanation

--------------------------------------------------------------------------------
  "                        '"'
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    [^"]+                    any character except: '"' (1 or more
                             times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  "                        '"'

See proof.

Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37
-1

This should be answer to your question.

function myFunction() {
  var fruits = ["Banana", "Orange", "Apple", "Mango"];
  var result = fruits.join(",");
}
peter novak
  • 19
  • 1
  • 5