Jump to content

Replacing multiple :) with Smileys


chrismead

Recommended Posts

I've run into a snag with a template where users are allowed to enter their own comment line.

 

Some people requested Smilies in the form of ":)" which I used a snippet of code to replace with ☺:

return Trim(Field("Comment").replace(":)","☺");

 

Someone just asked for a line of Smilies ":) :) :) :)"

and that code returned "☺:) :) :)".

 

No problem, I'm thinking, I'll just make it a global replace with /g:

return Trim(Field("Comment").replace(/:)/g,'☺');

 

Unfortunately, the close parenthesis refuses to register as a character in this format- an error message is returned when I try to validate: "Comment Rule, line 6: SyntaxError: unmatched ) in regular expression"

 

 

Using .replace(":)"/g,'☺') doesn't work because the expressions are mixed (I'm not sure if that's the right terminology).

 

Using .replace(/:()/g,'☺') generates valid code, but returns ☺)☺)☺)☺) (I did not expect it to give the right results, but it shows some of the internal process being applied).

 

I'm not at the end of my rope, but some of the solutions I'm thinking of are getting really complicated.

 

Does anyone have suggestions?

Link to comment
Share on other sites

Parentheses have special meaning in regular expressions. You need to escape them (and close the 'Trim' function):

return Trim(Field("Comment")[color="red"])[/color].replace(/:[color="Red"]\[/color])/g,'☺');

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...