简体   繁体   中英

combine two regular expressions into one

there I apply two regular expression to javascript string:

url.replace(/(^https?\:\/\/)?(www\.)?/i, '').replace(/\/$/, '')

how can I combine them into one replace function?

Since the replacement is the same, you can use alternation to match/replace "this" or "that":

 let url = 'http://www.example.com/'; console.log(url.replace(/^(?:https?\\:\\/\\/)?(?:www\\.)?|\\/$/gi, ''));

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