Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6551953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:24:02+00:00 2026-05-25T12:24:02+00:00

I need to create a faunctionality, that will allow me to parse a text

  • 0

I need to create a faunctionality, that will allow me to parse a text looking for a match for emoticon string in different format. It can be either a :) type emoticon, or a &lt;dog&gt; (<dog>) type.
Currently my function can find both types but needs two different regexes for this task, and I’d like to make it in just one step. Any ideas ?

Live example at jsfiddle: http://jsfiddle.net/rmQSx/5/

and the code :

function replaceEmoticons(text) {
    var emots1 = {
        ':)': 'smile.gif',
        ':(': 'sad.gif',
    },
    emots2 = {
        '&lt;dog&gt;': 'dog.gif',
        '&lt;fly&gt;': 'fly.gif'   
    },
    regex1,regex2,p,re;


    for(p in emots1){
        regex1 = "["+p+"]{2}";
        re = new RegExp(regex1, "g");

        text = text.replace(re, function (match) {
            return typeof emots1[match] != 'undefined' ?
                '<img src="'+emots1[match]+'"/>' :
                match;
        });
    }

    for(p in emots2){
        regex2 = "("+p+")(|$)";
        re = new RegExp(regex2, "g");

        text = text.replace(re, function(match) {
            return typeof emots2[match] != 'undefined' ?
                '<img src="'+emots2[match]+'"/>' :
                match;
        });
    }

     return text;   
}

console.log(replaceEmoticons('this is &lt;dog&gt;b a&lt;fly&gt; simple test :) , ;)   and more:):('));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T12:24:02+00:00Added an answer on May 25, 2026 at 12:24 pm

    I’m pretty sure you don’t need the funny stuff you do at the beginning of each for loop to modify the regex. Get rid of that, and there’s nothing stopping you from merging the two sets together.

    However, you should escape the special characters ( and ) in your smileys when making the regexes. Also, I would suggest making the regex search case-insensitive so that <dog> and <DOG> are both matched.

    //helper function to escape special characters in regex
    function RegExpEscape(text) {
        return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); 
    }
    
    function replaceEmoticons(text) {   
        var emots = {
            ':)'         : 'smile.gif',
            ':('         : 'sad.gif',
            '&lt;dog&gt;': 'dog.gif',
            '&lt;fly&gt;': 'fly.gif'
        }
    
        var result = text;
        var emotcode;
        var regex;
    
        for (emotcode in emots)
        {
            regex = new RegExp(RegExpEscape(emotcode), 'gi');
            result = result.replace(regex, function(match) {
                var pic = emots[match.toLowerCase()];
    
                if (pic != undefined) {
                    return '<img src="' + pic + '"/>';
                        } else {
                    return match;
                        }
                    });
        }
    
        return result;    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a python module that will be installed on end-user machines.
I am working on a site that will allow users to create an account.
I have a need to create a couple of classes that will serve as
I have the need to create the functionality to allow my registered users to
Need to create a custom DNS name server using C which will check against
I need to create an XML schema that looks something like this: <xs:element name=wrapperElement>
I need to create a backup of a SQL Server 2005 Database that's only
I need to create a simple windows based GUI for a desktop application that
I'm trying to create a function in C# which will allow me to, when
I'm trying to create a server control, which inherits from TextBox, that will automatically

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.