1

私は自分のドメインのこの文字列を持っています:

www.my.domain.com

出力を次のようにします。

my.domain

私は現在、次のようにしています:

str.replace("www.","").replace(".com","")

2つではなく1つだけの交換でどのように作ることができますか?

4

2 に答える 2

6

Try this regexp:

str.replace(/www\.|\.com/g, '');

If you're not familiar with the syntax |means "or", making it match the patterns on both sides. \.since period otherwise means "any character". /gmakes the matching and replacing "global", i.e. processes all occurrences. If you want it to be case insensitive you can use /gi instead.

于 2013-02-07T09:57:32.713 に答える
4

やるべきことは次のとおりです。

str.replace(/^www\.|\.com$/g, '');

開始位置と終了位置検索付き。

www.my.comedy-domain.com でうまくいかない

于 2013-02-07T10:05:35.760 に答える