我正在尝试创建正则表达式,只能在','
''
例如
Input - Sunday,Monday Output - Sunday,Monday
Input - Sunday, Output - Sunday
Input - ,Monday Output - Monday
答案 0 :(得分:2)
使用^
和$
仅在开头或结尾处匹配:
/^,|,$/
以下是Javascript示例:
'Sunday,Monday'.replace(/^,|,$/g, '') // => "Sunday,Monday"
'Sunday,'.replace(/^,|,$/g, '') // => "Sunday"
',Monday'.replace(/^,|,$/g, '') // => "Monday"