【发布时间】:2021-07-12 18:15:58
【问题描述】:
我有一组字符串
字符串要么以warning_with_ 开头,要么以breakdown_with_ 开头。我想提取出door_cycling,中间部分
或者warning_with_/breakdown_with_之后的文字
如果不匹配,则按原样返回字符串。
"warning_with_door_cycling_floor_n_door_b" --> regex should return ---- door_cycling
"breakdown_with_door_cycling_door_:door:" --> regex should return ---- door_cycling
"breakdown_with_door_cycling_door_a" --> regex should return ---- door_cycling
"breakdown_with_bumps_at_location" --> regex should return ---- bumps_at_location
"unknown_string" --> regex should return ---- unknown_string
到目前为止,这就是我所做的
const regex = /(warning_with_|breakdown_with_)(\w+?)(?:(_floor_|_door_).+)/;
// But it does not work for this case
"breakdown_with_bumps_at_location".replace(regex, '$2');
我错过了什么?
【问题讨论】:
标签: javascript reactjs regex