开发者

Match regex that doesn't start with something

I have seen a few other similar questions, but I can't seem to get it to work. What I am trying to do is in JavaScript match a string with issue numbers. I have that part working fine, but I don't want to match it if it starts with target='_blank'>.

These should match...

Issue# 123  
Issue #123  
Issue 123  
Issue no 123  
Issue number 123  
Issue: 123  

These should not...

target='_blank'>Issue# 123  
target='_blank'>Issue #123  
target='_blank'>Issue 123  
target='_blank'>Issue no 123  
target='_blank'>Issue number 123  
target='_blank'>Issue: 123

What I have so far is

(?!target='_blank'>)(issue(?: number| no)?[ #:]?[ #]?([0-9]+))

But it is still matching the ones it shouldn't.开发者_Go百科

Any help would be great.


You're using negative look ahead when you should be using negative look behind.

(?<!target='_blank'>)(issue(?: number| no)?[ #:]?[ #]?([0-9]+))

Note the ?<! instead of ?!.


Try this: ^(Issue(?: number| no)?[ #:]?[ #]?([0-9]+)) (Untested)

If you're trying to parse HTML with Regex. Please don't, instead.. Use DOM


Glossing over the usual speech about "don't parse HTML with regexes",... if you're using jQuery, you could do something like

$("a[target!='_blank']").each(function() {
    if(/yourregex/.match($(this).text()) {
        // do whatever
    }
});

That probably "won't compile", but that's the general idea.


Thanks guys,

What I've ended up having to do is as suggested here, and reverse the string and my regex, and use look ahead.

So my new regex is

(([0-9]+)[ #]?[ #:]?(?:rebmun |on )?eussi)(?!>'knalb_'=tegrat)

lame.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜