I am attempting to generate links from specific strings. My current approach is:
([^&]#)([0-9]+)(?![^<>]*>)
However, the results are not entirely accurate.
Test 1: j
Test 2: #1040
Test 3: some text followed by #1060
Test 4: <a href="#1060">#1060</a>
Test 5: <b>#1078</b> (...or any other tag except <a>)
- Test
1
&4
: should NOT match. - Tests
2
,3
&5
: should match.
The goal is to match a #number
within any tag except for a link tag (and its attributes).
You can experiment with this in the following jsfiddle: http://jsfiddle.net/xqnjs2uq/3/
Considered as "regex3", the regex pattern plays an integral role alongside two others in achieving the desired outcome.
UPDATE Thanks to @chsdk's assistance, I've found a solution using two separate regex's (refer to regex3 and regex4 in the linked jsfiddle)...
http://jsfiddle.net/xqnjs2uq/6/
/[^&|href="]+(#[0-9]+\b)/gim --> matches Test 3 and 5 above
/^(#[0-9]+)/ --> needed to match Test 2
The ultimate challenge remains - Can this be condensed into a single regex?