locked
Find Exact String of Characters in Sentence RRS feed

  • Question

  • I'm trying to replace smiley characters in a string for my chat program and replace them with the correct image.  I thought I had it working but only if it is the only characters in the string.  How can I find the characters in a sentence, has to match exactly too.

    This is my current regex

            str1 = "^\<\:\-O$";
            var re = new RegExp(str1, "g");
            cleanmsg = cleanmsg.replace(re, '<img class="smiley" src="smileys/'+smileys[index]+'.png" alt="'+index+'">')

     

    Example of what I'm trying to do:

    test sentence :-) find multiple :-) here

    returns

    test sentence <img> find multiple <img> here

    • Moved by SamAgain Friday, June 25, 2010 10:25 AM (From:Regular Expressions)
    Thursday, June 17, 2010 2:35 PM

All replies

  • Hi,

    that's JavaScript - this is a .NET - Forum...

     

    Whatever, with ^ you search for a string at the beginning of the input and $ is the end of the input. Remove these characters from your pattern.

    I don't know, what you match with the rest \<\:-O, but if it matches what you want if it's the only characters, then without ^ and $ it matches any occurence in any sentence. 

    Greetings,


    Wolfgang Kluge
    gehirnwindung.de
    • Proposed as answer by Waratah Friday, June 25, 2010 12:50 AM
    Thursday, June 17, 2010 3:30 PM
  • well I guess I should have left out the javascript but I wanted to show how I was using the regex.  Yeah I removed that and it works but I had to add a space before all my emoticons so i could distinguish between :-) and <:-) so I make added a \s to the beginning of the expression.  Thanks for the help
    Thursday, June 17, 2010 6:12 PM
  • Hi,

    there are different regex-flavors out there. The JavaScript-Engine is different to the one of .NET - especially in many detail..

    Please mark a reply as answer if it answers your question.

    Greetings,

    Wolfgang Kluge
    gehirnwindung.de
    Thursday, June 17, 2010 7:46 PM