Home  >  Q&A  >  body text

Remove quotes from strings in ReactJS: Guide

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粉165522886P粉165522886370 days ago493

reply all(1)I'll reply

  • P粉680087550

    P粉6800875502023-09-18 12:08:24

    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"
    ]
    */
    

    reply
    0
  • Cancelreply