简体   繁体   English

将两个正则表达式合二为一

[英]combine two regular expressions into one

there I apply two regular expression to javascript string:在那里我将两个正则表达式应用于 javascript 字符串:

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

how can I combine them into one replace function?如何将它们组合成一个replace功能?

Since the replacement is the same, you can use alternation to match/replace "this" or "that":由于替换是相同的,您可以使用交替来匹配/替换“this”或“that”:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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