私は自分のドメインのこの文字列を持っています:
www.my.domain.com
出力を次のようにします。
my.domain
私は現在、次のようにしています:
str.replace("www.","").replace(".com","")
2つではなく1つだけの交換でどのように作ることができますか?
私は自分のドメインのこの文字列を持っています:
www.my.domain.com
出力を次のようにします。
my.domain
私は現在、次のようにしています:
str.replace("www.","").replace(".com","")
2つではなく1つだけの交換でどのように作ることができますか?
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". /g
makes the matching and replacing "global", i.e. processes all occurrences. If you want it to be case insensitive you can use /gi
instead.
やるべきことは次のとおりです。
str.replace(/^www\.|\.com$/g, '');
開始位置と終了位置検索付き。
www.my.comedy-domain.com でうまくいかない