JS正则表达式查找替换URL的协议,并从字符串到字符串结尾

问题描述

例如,我有以下字符串:

https://variant-color.com

http://variant-size.biz

http://variant-type.ca

我想使用JS variant-和正则表达式删除URL的协议.replace()和域,因此字符串为:

颜色

大小

类型

该怎么做?我已经用以下代码尝试过了,但是没有用:

.replace("http://variant-","").replace("https://variant-","").replace(/^.[a-zA-Z]*/,"")

解决方法

嗯,有多种方法可以做到这一点。最简单的方法之一是在此正则表达式中使用positive look behind方法:

df = df.assign(count=df.groupby(['ID','City'])['City'].transform('count')).drop_duplicates()
df['max'] = np.where((df['count'] == df.groupby('ID')['count'].transform('min')),'third_frequent_city',np.nan)
df['max'] = np.where((df['count'] == df.groupby('ID')['count'].transform('median')),'second_frequent_city',df['max'])
df['max'] = np.where((df['count'] == df.groupby('ID')['count'].transform('max')),'first_frequent_city',df['max'])
df = df.drop('count',axis=1).set_index(['ID','max']).unstack(1)

仅当您的URL中的字符后跟 City max first_frequent_city second_frequent_city third_frequent_city ID 1 London New York Berlin 2 Shanghai NaN NaN 符号时,此字符才会匹配。

所以最终输出将是这样的:

/(?<=\-)\w+/gm