简体   繁体   中英

Regex match after @ but not include and before / but not include

I am a regex noob and I try this for a long time.

case1: postgres://123:123@localhost/123
case2: postgres://123:123@integration/123

I am trying to find a regex that matches after @ and before /

For case 1 will give me localhost, case 2 will give me integration

Thanks!

Just process the match to remove the delimiter(s). There is not much else you can do since JavaScript regex doesn't support lookahead. This should do it.

 var strs = ["postgres://123:123@localhost/123", "postgres://123:123@integration/123"]; strs.forEach(str => console.log(str.match(/(?=@)[^\\/]+/)[0].substr(1))); 

What this does is to return a match that includes the @ and then process that match to remove the delimiter.

You may want to check if there are any matches before you do this to avoid exceptions.

 var test = [ 'postgres://123:123@localhost/123', 'postgres://123:123@integration/123' ]; console.log(test.map(function (a) { return a.match(/@(\\w+)/)[1]; })); 

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