skip to Main Content

i am new to javascript. i have paragraph and split them using str.split(‘.’). Also i need to remove quotation marks from string after split method. How to eliminate them?

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.”

and i want the result like

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.

2

Answers


  1. It would be easier to remove all quotation marks 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, you should loop/map over each sentence after you .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, you end up with an empty string as the last item. To get rid of that you could also use .filter().

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

    To get rid of the whitespace, you could also use .trim().

    So to put all of this 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"
    ]
    */
    
    Login or Signup to reply.
  2. You can use .replace(),

    const p = '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.”';
    
    const regex = /“|”/g;
    
    console.log(p.replace(regex, ''));
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search