By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,400 Members | 1,334 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,400 IT Pros & Developers. It's quick & easy.

Regex question

Jezternz
100+
P: 145
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:white">me</span>'

I would use:
Expand|Select|Wrap|Line Numbers
  1. editable_div.innerHTML = editable_div.innerHTM.replace(/me/gi, "<span style=\"color:white\">me</span>");
  2.  
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:
Expand|Select|Wrap|Line Numbers
  1. <span style="color:white"><span style="color:white"><span style="color:white">me</span></span></span>
  2.  
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
Aug 11 '09 #1
Share this Question
Share on Google+
3 Replies


Jezternz
100+
P: 145
Well I have found that you can use the expression found here:
http://www.w3schools.com/jsref/jsref...glookahead.asp
Expand|Select|Wrap|Line Numbers
  1. /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.
Aug 12 '09 #2

acoder
Expert Mod 15k+
P: 16,027
For regular expressions, you should find this site useful.
Sep 5 '09 #3

Post your reply

Sign in to post your reply or Sign up for a free account.