[英]Regex to extract LHS and RHS of a char
我正在為以下問題尋找正則表達式 -
字符串 - "32 - This-may-contain - hypen-as-well"
從"-"
除
結果-> "32 "
, " This-may-contain - hypen-as-well"
我正在做"32 - This-may-contain-hypen-as-well".split("-");
但是 RHS 也可以包含 hypen,我需要做更多的操作才能用 hypen 加入 rhs。
在比賽中使用如下正則表達式
const input = "32 - This-may-contain - hypen-as-well"; const result =input.match(/(.*?) - (.*)/).slice(1); console.log(result);
但沒有正則表達式它仍然可以完成
const input = "32 - This-may-contain - hypen-as-well"; const [LHS, ...RHS] = input.split(' - '); const result = [LHS, RHS.join(' - ')]; console.log(result);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.