简体   繁体   中英

negative lookbehind and lookahead

i want to get an Website out of a string, so I had writen an RegEx pattern using match()

var websiteOutput = string.match(/[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi);

The Problem now is, the string also contains E-Mail Adresses, but I don't want to match these, so I thought about an negative lookbehind and lookhead for '@', but I'm not sure how to implement it.

Should not match, but match:

    test.test@test.com
    test@test.com

Hope you can help me, Greetings TZimon

Instead of over complicating the regex , you can try a simpler approach :

if(!/@/gi.test(string)){
    var websiteOutput = string.match(/[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi);
}

ie accept only the strings which do not have @ in them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM