Remove quotes from strings in ReactJS: Guide
P粉165522886
P粉165522886 2023-09-17 23:46:44
0
1
570

I just started learning JavaScript. I have a paragraph that I split using str.split('.'). Additionally, I need to remove quotes from the split string. How to remove them?

My mother stood up and picked up a box from the ground. "We're in America, Rune. They speak English here. You've been speaking English, just like you've been speaking Norwegian. It's time to use English."

I hope the result is as follows:

My mother stood up and picked up a box from the ground. We're in America, Rune. They speak English here. You've been speaking English just as much as you've been speaking Norwegian. It's time to use English.

P粉165522886
P粉165522886

reply all(1)
P粉680087550

It would be easier to remove all quotes before splitting the array.

const paragraph = `My mamma stood up and lifted a box off the ground. “We’re in America, Rune. They speak English here. You’ve been speaking English for as long as you’ve been speaking Norwegian. It’s time to use it.”`.replace(/“|”/g,'');

console.log(paragraph);
// "My mamma stood up and lifted a box off the ground. We’re in America, Rune. They speak English here. You’ve been speaking English for as long as you’ve been speaking Norwegian. It’s time to use it."

If you insist on splitting the array first, then you should loop/map each sentence after .split.

const sentences = `My mamma stood up and lifted a box off the ground. “We’re in America, Rune. They speak English here. You’ve been speaking English for as long as you’ve been speaking Norwegian. It’s time to use it.”`.split('.');

const result = result = sentences.map(sentence => sentence.replace(/“|”/g,''));

console.log(result);
/*
[
   "My mamma stood up and lifted a box off the ground",
   " We’re in America, Rune",
   " They speak English here",
   " You’ve been speaking English for as long as you’ve been speaking Norwegian",
   " It’s time to use it",
   ""
];
*/

As you can see, the last item is the empty string. To remove it you can also use .filter().

result = sentences.map(sentence => sentence.replace(/“|”/g,'')).filter(sentence => sentence);

To remove spaces, you can also use .trim().

So, putting it all together:

const sentences = `My mamma stood up and lifted a box off the ground. “We’re in America, Rune. They speak English here. You’ve been speaking English for as long as you’ve been speaking Norwegian. It’s time to use it.”`.split('.');

const result = sentences
  .map(sentence => sentence.replace(/“|”/g, '').trim())
  .filter(sentence => sentence);

console.log(result);

/*
[
  "My mamma stood up and lifted a box off the ground",
  "We’re in America, Rune",
  "They speak English here",
  "You’ve been speaking English for as long as you’ve been speaking Norwegian",
  "It’s time to use it"
]
*/
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template