effel: ein hartes blank( ) einfügen

Einfügen eines   um einen Zeilenumbruch zu vermeiden.

das geht:

// Zuweisen des Unicode-Zeichens für ein geschütztes Leerzeichen
element.textContent = 'Hallo\u00A0Welt'; // Fügt ein geschütztes Leerzeichen ein

Diese Einfügung funktioniert nicht:

text=element.textContent 
alert("text "+text);        // -> text Hallo Welt (noch kein   eingefügt)

text= text.replace(/ /gi,'\\u00A0');

in / / steht ein einfaches blank

alert("text "+text);   // -> text Hallo\u00A0Welt UniZeichen \u00A0 für  
 
element.textContent=text;

alert(element.textContent) //Hallo\u00A0Welt

so sieht es in der Darstellung in der Webseite aus://Hallo\u00A0Welt

Wer kann mir das erklären

Danke für Eure Hilfe

Effel

ein gesegnetes und erfolgreiches 2026

  1. Moin effel,

    bitte in Zukunft Code auch entsprechend auszeichnen. Ich habe das für Dich einmal getan und damit auch gleichzeitig die Les- und Nachvollziehbarkeit deutlich erhöht.

    Keine Ursache,
    Robert

  2. @@effel

    Diese Einfügung funktioniert nicht:

    text= text.replace(/ /gi,'\\u00A0');
    

    Da ist ein \ zu viel. So escapest du das \ und erhältst den String '\u00A0', nicht das Zeichen U+00A0.

    Mit nur einem \ funktioniert’s, guckst du.

    🖖 Live long and prosper

    --
    “In my home, the America I love, the America I've written about, that has been a beacon of hope and liberty for 250 years, is currently in the hands of a corrupt, incompetent and treasonous administration. Tonight, we ask all who believe in democracy and the best of our American spirit, to rise with us, raise your voices against authoritarianism, and let freedom reign.”
    — Bruce Springsteen, Manchester 2025-05-14
    1. @@Gunnar Bittersmann

      text= text.replace(/ /gi,'\\u00A0');
      

      Da ist ein \ zu viel.

      Und du brauchst auch keine RegExp dafür. Ein einfacher String ' ' genügt:

      text = text.replaceAll(' ', '\u00A0');
      

      Im Codepen geändert.

      🖖 Live long and prosper

      --
      “In my home, the America I love, the America I've written about, that has been a beacon of hope and liberty for 250 years, is currently in the hands of a corrupt, incompetent and treasonous administration. Tonight, we ask all who believe in democracy and the best of our American spirit, to rise with us, raise your voices against authoritarianism, and let freedom reign.”
      — Bruce Springsteen, Manchester 2025-05-14
      1. Hallo Gunnar,

        das war falsch: text.replace(/ /gi,'\\u00A0');

        so war es richtig: text.replace(/ /gi,'\u00A0');

        es geht auch so: text.replaceAll(/ /gi,'\u00A0');

        der eigentliche Fehler war: ich habe <meta http-equiv="Content-Type" content="text/html; charset=UTF-8 "> vergessen.

        alles wieder ok Danke

        Effel

        1. @@effel

          das war falsch: text.replace(/ /gi,'\u00A0');

          so war es richtig: text.replace(/ /gi,'\u00A0');

          Hä??

          Ach so, du hast dein Posting unleserlich gemacht, weil du Code nicht als solchen ausgezeichnet hast. Ich korrigier das mal eben. Das nächste Mal bitte selber.

          es geht auch so: text.replaceAll(/ /gi,'\u00A0');

          Ich hatte dir doch gezeigt, wie es auch geht: ohne Regexp. Das spart ein paar Mikrosekunden Rechenzeit und ein paar Gramm CO₂.

          der eigentliche Fehler war: ich habe <meta http-equiv="Content-Type" content="text/html; charset=UTF-8 "> vergessen.

          Die Angabe sollte nicht erforderlich sein. Es sei denn, dein Server ist so konfiguriert, dass er anstatt keiner Angabe zur Zeichencodierung eine falsche macht.

          Außerdem ist sie unnötig lang; <meta charset="UTF-8"> tut’s auch.

          🖖 Live long and prosper

          --
          “In my home, the America I love, the America I've written about, that has been a beacon of hope and liberty for 250 years, is currently in the hands of a corrupt, incompetent and treasonous administration. Tonight, we ask all who believe in democracy and the best of our American spirit, to rise with us, raise your voices against authoritarianism, and let freedom reign.”
          — Bruce Springsteen, Manchester 2025-05-14