I'm trying to create links from certain strings. I've tried this:
([^&]#)([0-9]+)(?![^<>]*>)
But it's not working quite correctly.
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. - Test
2,3&5: should match.
I.e. It should match for a #number in any tag except for a link tag (nor it's attributes).
Here's a jsfiddle where you can test it out: http://jsfiddle.net/xqnjs2uq/3/
The regex in question is labelled "regex3" I've included two other regex's in there because their part of the bigger picture – just to be able to check whether this regex interferes with any of those.
UPDATE With help from @chsdk I've managed to find a way to do this with two regex's (see regex3 and regex4 in the following jsfiddle)...
http://jsfiddle.net/xqnjs2uq/6/
/[^&|href="]+(#[0-9]+\b)/gim --> matches Test 3 and 5 above
/^(#[0-9]+)/ --> required to match Test 2 above
1st prize would be for condensing this into a single regex. Is that possible?


(^#|[^&]#)([0-9]+)(?![^<>]*[<>])?(^#|[^&\n]#)([0-9]+)(?![^<>\n]*(?:<\/a>|[^>\n]*>[^<\n]*<\/a>))then?