1

I have built a text flipper in JavaScript for a bookmarklet.

It works fine until it sees the letter 'l'; at which point it starts a new line and it breaks the sentence. (A terminating period, question mark or exclamation point do the same thing unless they are in the middle of a larger body of text, in which case they behave nicely.)

I have tried escaping the \l I have tried breaking it out and replacing it individually: str.replace(/\l/g,'ן');

Nothing seems to work. Other than that and the (.?!) - all works well.

I know that l: creates a new line and I'm guessing that's what's happening.

https://jsbin.com/qaririjulo/edit?html,output

function flipText() {
  var x = document.getElementById("myTextarea").value;
  x = x.split("").reverse().join("");
  x = x.toLowerCase();

  var replaceChars = {
    'a': 'ɐ',
    'b': 'q',
    'c': 'ɔ',
    'd': 'p',
    'e': 'ǝ',
    'f': 'ɟ',
    'g': 'b',
    'h': 'ɥ',
    'i': 'ı',
    'j': 'ظ',
    'k': 'ʞ',
    'l': 'ן',
    'm': 'ɯ',
    'n': 'u',
    'o': 'o',
    'p': 'd',
    'q': 'b',
    'r': 'ɹ',
    's': 's',
    't': 'ʇ',
    'u': 'n',
    'v': 'ʌ',
    'w': 'ʍ',
    'x': 'x',
    'y': 'ʎ',
    'z': 'z',
    '\?': '¿',
    '!': '¡',
    '\,': '\'',
    '\'': '\,',
    '\.': '˙'
  };
  x = x.replace(/a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z|!|\,|\'|\?|\./g, function(match) {
    return replaceChars[match];
  })

  document.getElementById("target").innerHTML = x;
}
<textarea id="myTextarea" placeholder="Enter Your Text Here" rows="3" cols="30"></textarea> <textarea id="target" placeholder="Flipped Text Will Appear Here" rows="3" cols="30" style="direction: rtl;">
</textarea>
<br />
<button type="button" onClick="flipText()">Flip it</button>
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
DanDel
  • 11
  • 3
  • 2
    The code you've shared here does not produce a new line with the lowercase ell. – Heretic Monkey Apr 14 '19 at 16:37
  • @CodeManiac, do you mean "unable"? Because your result looks fine. – Andy Apr 14 '19 at 16:53
  • @Andy yeah sorry typo, i meant not able to reproduce – Code Maniac Apr 14 '19 at 16:54
  • 2
    I agree with these findings. I can't reproduce this issue. – Andy Apr 14 '19 at 16:55
  • 1
    And i will suggest using `character class` instead of `alternation`, you can change your regex to `[a-z!,'?.]` instead of `/a|b|c...` – Code Maniac Apr 14 '19 at 17:00
  • For me every letter works fine except the letter corresponding to 'j' and 'l'. Normally all letters are turned upside down and are written from right to left. But writing one of them prints them always at the start of the string and changes the print direction of all subsequent letters from left to right. – Peter Lehnhardt Apr 14 '19 at 21:41

0 Answers0