O. Shekriladze
O. Shekriladze

Reputation: 1536

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?

Upvotes: 1

Views: 119

Answers (1)

MonkeyZeus
MonkeyZeus

Reputation: 20747

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, ''));

Upvotes: 1

Related Questions