-1

I'm trying to replace smiles in a string with Javascript and this is a line from my code:

res = res.replace(/;\)/g, function myFunction(x){
                               return '<img src="/emoticons/wink.png" >'; 
                          });

It's working fine and when I send ;) it gives me the smile

But the Problem is that when I send '); it works also and give me the same smile

I mean when I send for example Simplemessage('sometext'); it change it to example Simplemessage('sometext'{Smile};

Khalil Bz
  • 557
  • 1
  • 9
  • 24
  • A list but when I just stop that specific line every thing change to be working => The problem in that Line (Thanks in advance) – Khalil Bz Oct 08 '16 at 18:01
  • Sorry I'm not good in Regrex so could you write the example and I will try it (Thank you so much for your help) – Khalil Bz Oct 08 '16 at 18:04
  • 5
    `'` probably gets converted to `'`, effectively turning this into `');`. – Sebastian Simon Oct 08 '16 at 18:04
  • Is `res` a HTML string? [You can't parse HTML with regex](http://stackoverflow.com/a/1732454/1529630) – Oriol Oct 08 '16 at 18:06
  • @Xufox You are really a Genius, HHHHHHHHHHHH that's it that's the problem it's JSON – Khalil Bz Oct 08 '16 at 18:06
  • Note you should only use regex on a *plain* text. – Wiktor Stribiżew Oct 08 '16 at 18:07
  • @Xufox Thank you so much for your help, But what to do, Could you help please :) – Khalil Bz Oct 08 '16 at 18:09
  • @KhalilBz I’m not exactly sure what the process for displaying the message is, but I would make that replacement, before the string gets added to HTML. It looks like you’re getting `res` from the `innerHTML` of some element; I’d do it before setting the `innerHTML`. But I can’t see the details, so I can’t say for sure. – Sebastian Simon Oct 08 '16 at 18:12
  • @Xufox It's converted to JSON in PHP and After getting the Ajax Request I directly replace all the smiles, And thank you so much, Please answer with your comment to mark it as best answer if you want ^_^ – Khalil Bz Oct 08 '16 at 18:25
  • @Xufos that was amazing, it'd have taken me days. – Addison Oct 14 '16 at 04:39

1 Answers1

1

Please excuse how messy this is - but I assure you that this is about as good as you're going to get.

(?<!&#([0-46-9]\d|5[0-8]))(?<!&#\d{3})(?<!&[a-z]{2})(?<!&[a-z]{3})(?<!&[a-z]{4})(?<!&[a-z]{5})(?<!&[a-z]{6})(?<!&[a-z]{7})(?<!&[a-z]{8});\)

Basically it makes sure that the ) is not preceded by an alternate symbol, such as &apos; or &#39;, except for the unique case in which it is &#59;, which is the symbol for semicolon (and thus, it should work.

Since it uses a negative lookbehind, I have to (unfortunately) duplicate a piece of the code, as it cannot be variable length. The longest ascii entity in html is thetasym, which is 8 characters long, so I must duplicate it that many times.

Example: https://regex101.com/r/hkJ9yT/1

Let me know if this one works! (and massive kudos to Xufos for figuring out the &apos; thing)

Addison
  • 7,322
  • 2
  • 39
  • 55