1

I'm trying to create links from certain strings. I've tried this:

([^&]#)([0-9]+)(?![^<>]*>)

But it's not working quite correctly.

Test 1: &#106
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?

6

3 Answers 3

2

Simply use the following regex /[^&|href="](#[0-9]+)/g.

This is a live DEMO, giving those results:

enter image description here

With the following matches:

enter image description here

EDIT:

I missed the part where you want to avoid matching this expression within links, this is the regex you will need for this:

[^&|"](#[0-9]+\b)(?!<\/a>)

We added the \b here to make sure it takes the whole expression at a word boundary and (?!<\/a>) to avoid matching links.

And this is the Live DEMO again, and you can test it in the following Snippet:

var re = /[^&|"](#[0-9]+\b)(?!<\/a>)/g;
var str = 'Test 1: &#106\nTest 2: #1040\nTest 3: some text followed by #1060\nTest 4: <a href="#1060">#1060</a>\nTest 5: <b>#1078</b> (...or any other tag except <a>)';
var m;

while ((m = re.exec(str)) !== null) {
  if (m.index === re.lastIndex) {
    re.lastIndex++;
  }
  document.write("<b>" + m[1] + "</b><br/>");
}

Sign up to request clarification or add additional context in comments.

6 Comments

This isn't working for me. It's eating closing brace of tags. E.g. <p<a href="#/app/job/1078/1" target="_blank">#1078</a></p>. Note the broken p tag. This is after using replace on <p>#1078</p> using that regex.
This fiddle is broken, but just use the code in my snippet to replace it,
I have: jsfiddle.net/7acfut1r/1 ... but it still doesn't work. Please notice how the <p> tag has become <p (without the closing brace).
@geoidesic Yes I see, the problem is that the regex is matching the sequence group but capturing the previous character too, so the solution is to use regex3.exec() method like this match = regex3.exec(string)[1] and then use match variable in your replace, this is the updated fiddle, I hope it helps.
Thx @chsdk. Seems to work for some strings but not others. I've updated the jsfiddle with all the tests: jsfiddle.net/xqnjs2uq/5.
|
1

Try (^(#|(<[b-zB-Z]+>))((0-9)+)(?!(<\/*a>*)))

Comments

0

Here's my solution ^^. Hope this help you

/#[0-9]+/gi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.