Regex question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jezternz
    New Member
    • Jan 2008
    • 145

    Regex question

    Ok, basically I am currently building a kind of script editor, and I want smart highlighting, so as the user types I want to highlight specific words and symbols.
    I also want this to be realtime.
    So as the user types the innerHTML property is updated using the string replace method. So say I want to change all 'me' s with '<span style="color:wh ite">me</span>'

    I would use:
    Code:
    editable_div.innerHTML = editable_div.innerHTM.replace(/me/gi, "<span style=\"color:white\">me</span>");
    The only problem with this offcourse is, that as you type, every me will get another set of tags, so after 3 edits after a me has been replaced, the html will end up as:
    Code:
    <span style="color:white"><span style="color:white"><span style="color:white">me</span></span></span>
    So I need to know how to basically say "If a span isn't already around this portion of the string add a span, otherwise do not match the regular expression".

    Thanks, Josh
  • Jezternz
    New Member
    • Jan 2008
    • 145

    #2
    Well I have found that you can use the expression found here:

    Code:
    /regexp(?!ahead)/
    I am really not sure if this can be done in other regular expression evaluation engines (as I couldn't find it in Google under general regex), but it seems to work in javascript.

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      For regular expressions, you should find this site useful.

      Comment

      • EisBear
        New Member
        • Sep 2009
        • 3

        #4
        Or this:

        Comment

        Working...