Javascript Regex for Javascript Regex and Digits

powdyersazpj80

New Member
The title might seem a bit recursive, and indeed it is.I am working on a Javascript which can highlight/color Javascript code displayed in HTML. Thus, in the Internet Browser, comments will be turned green, definitions (for, if, while, etc.) will be turned a dark blue and italic, numbers will be red, and so on for other elements. However, the coloring is not all that important.I am trying to figure out two different regular expressions which have started to cause a minor headache.1. Finding a regular expression using a regular expressionI want to find regular expressions within the script-tags of HTML using a Javascript, such as:\[code\] match(/findthis/i);\[/code\], where the regex part of course is "/findthis/i".The rules are as follows:[*]Finding multiple occurrences (/g) is not important.[*]It must be on the same line (not /m).[*]Caseinsensitive (/i).[*]If a backward slash (ignore character) is followed directly by a forward slash, "/", the forward slash is part of the expression - not an escape character. E.g.: \[code\]/itdoesntstop\/untilnow:/\[/code\][*]Two forward slashes right next to each other (//) is: (A) At the beginning: Not a regex; it's a comment. (B) Later on: First slash is the end of the regex and the second slash is nothing but a character.[*]Regex continues until the line breaks or end of input (\n|$), or the escape character (second forward slash which complies with rule 4) is encountered. However, also as long as only alphabetic characters are encountered, following the second forward slash, they are considered part of the regex. E.g.: \[code\]/aregex/allthisispartoftheregex\[/code\]So far what I've got is this:\[code\] '\\/(?:[^\\/\\\\]|\\/\\*)*\\/([a-zA-Z]*)?'\[/code\]However, it isn't consistent. Any suggestions?2. Find digits (alphanumeric, floating) using a regular expressionFinding digits on their own is simple. However, finding floating numbers (with multiple periods) and letters including underscore is more of a challenge.All of the below are considered numbers (a new number starts after each space):\[code\]3 3.1 3.1.4 3a 3.A 3.a1 3_.1\[/code\]The rules:[*]Finding multiple occurrences (/g) is not important.[*]It must be on the same line (not /m).[*]Caseinsensitive (/i).[*]A number must begin with a digit. However, the number can be preceeded or followed by a non-word (\W) character. E.g.: "=9.9;" where "9.9" is the actual number. "a9" is not a number. A period before the number, ".9", is not considered part of the number and thus the actual number is "9".[*]Allowed characters: [a-zA-Z0-9_.]What I've got:\[code\]'(^|\\W)\\d([a-zA-Z0-9_.]*?)(?=([^a-zA-Z0-9_.]|$))'\[/code\]It doesn't work quite the way I want it.
 
Back
Top