Heim  >  Fragen und Antworten  >  Hauptteil

Entfernen Sie Anführungszeichen aus Zeichenfolgen in ReactJS: Guide

Ich habe gerade angefangen, JavaScript zu lernen. Ich habe einen Absatz, den ich mit str.split('.') geteilt habe. Außerdem muss ich Anführungszeichen aus der geteilten Zeichenfolge entfernen. Wie entferne ich sie?

Meine Mutter stand auf und hob eine Kiste vom Boden auf. „Wir sind in Amerika, Rune. Hier wird Englisch gesprochen. Du sprichst Englisch, genau wie du Norwegisch sprichst. Es ist Zeit, Englisch zu verwenden

Ich hoffe, das Ergebnis sieht wie folgt aus:

Meine Mutter stand auf und hob eine Kiste vom Boden auf. Wir sind in Amerika, Rune. Hier wird Englisch gesprochen. Sie sprechen genauso viel Englisch wie Norwegisch. Es ist Zeit, Englisch zu verwenden.

P粉165522886P粉165522886370 Tage vor494

Antworte allen(1)Ich werde antworten

  • P粉680087550

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

    在拆分数组之前,去除所有引号会更容易。

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

    如果您坚持先拆分数组,那么您应该在.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",
       ""
    ];
    */
    

    如您所见,最后一个项是空字符串。要去除它,您还可以使用.filter()

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

    要去除空格,您还可以使用.trim()

    因此,将所有这些放在一起:

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

    Antwort
    0
  • StornierenAntwort