Javascript 如何从正则表达式中提取匹配项?

Javascript 如何从正则表达式中提取匹配项?,javascript,regex,Javascript,Regex,我有以下格式的文本: let text = "help me on monday, january 8 take the dog out"; 基本上,一个句子可能有一个组合 我想 识别句子是否包含工作日日期组合 提取工作日日期组合(因此周一,1月8日) 删除工作日名称(因此为1月8日) 对于1,我能够通过以下方式实现这一点 const weekdayRegex = /\b((mon|tues|wed(nes)?|thur(s)?|fri|sat(ur)?|sun)(day)?)\b/; con

我有以下格式的文本:

let text = "help me on monday, january 8 take the dog out";
基本上,一个句子可能有一个
组合

我想

  • 识别句子是否包含工作日日期组合
  • 提取工作日日期组合(因此周一,1月8日)
  • 删除工作日名称(因此为1月8日)
  • 对于1,我能够通过以下方式实现这一点

    const weekdayRegex = /\b((mon|tues|wed(nes)?|thur(s)?|fri|sat(ur)?|sun)(day)?)\b/;
    const monthThenDayRegex = /(jan(uary)?|feb(ruary)?|mar(ch)?|apr(il)?|may|jun(e)?|jul(y)?|aug(ust)?|sep(tember)?|oct(ober)?|nov(ember)?|dec(ember)?) *\d{1,2}(th|nd|st|rd){0,1}/;
    const dayDayThenMonthRegex = /d{1,2}(th|nd|st|rd){0,1} *(of){0,1} *(jan(uary)?|feb(ruary)?|mar(ch)?|apr(il)?|may|jun(e)?|jul(y)?|aug(ust)?|sep(tember)?|oct(ober)?|nov(ember)?|dec(ember)?)/;
    
    然后:

    这行得通,我明白了

    对于步骤2,如何提取“1月8日,星期一”

    对于步骤3,我如何保持“1月8日”

    您可以使用

    const text = "help me on monday, january 8 take the dog out";    
    const weekdayRegex = /\b((mon|tues|wed(nes)?|thur(s)?|fri|sat(ur)?|sun)(day)?)\b/;
    const monthThenDayRegex = /(jan(uary)?|feb(ruary)?|mar(ch)?|apr(il)?|may|jun(e)?|jul(y)?|aug(ust)?|sep(tember)?|oct(ober)?|nov(ember)?|dec(ember)?) *\d{1,2}(th|nd|st|rd){0,1}/;
    const commaSeperated = new RegExp(weekdayRegex.source + " *,{0,1} *" + monthThenDayRegex.source);
    
    
    expect(commaSeperated.test(text)).toEqual(true);
    expect(text.match(commaSeperated)[0]).toEqual('monday, january 8');
    expect(text
           .match(commaSeperated)[0]
           .match(monthThenDayRegex)[0]).toEqual('january 8');
    

    我对您的
    commaSeparated
    正则表达式做了一个小小的简化-
    {0,1}
    相同吗?

    let text=“1月8日星期一帮我把狗带出去”;
    const weekdayRegex=/\b(星期一、二、三、四、五、六、日);
    康斯特蒙特亨达雷格克斯=/(一月(uary)?二月(ruary)?三月(ch)?四月(il)?五月(e)?六月(y)?七月(y)?八月(ust)?九月(tember)?十月(ober)?十一月(ember)?十二月(ember)?*\d{1,2}(th nd{st}rd){0,1};
    康斯特·戴德蒙特勒日=/d{1,2}(th | nd | st | rd){0,1}*(一月(uary){2月(乡村)}三月(ch){4月(il)}五月(六月(e)}七月(y)}八月(ust){9月(tember)}十月(ober)}十一月(ember)}十二月(ember)//;
    const commaSeperated=new RegExp(weekdayRegex.source+“*,?*”+monthhendayregex.source);
    const m=text.match(CommaseOperated);
    如果(m){
    console.log(m[0]);
    console.log(m[0]。替换(/^.+,*/,“”))
    }否则{
    console.log(“不匹配”);
    }
    我简化了(复杂的?)您的正则表达式,方法是将
    monthhendayregex
    daythenmonthregex
    拆分为单独的
    monthRegex
    daytofmonthregex
    正则表达式,然后在
    dateRegex
    中再次将它们连接在一起。我还添加了前面的
    ^.*?
    和后面的
    *$
    来匹配字符串的开头和结尾,以允许使用来提取日期部分。如代码段所示,您还可以在处理前使用
    dateRegex.test()
    检查字符串是否与regexp匹配

    const weekdayRegex=/\b((?:周一周二周三周四周六周五周日);
    康斯特-蒙特勒日=/\b(一月(三月)→二月(三月)→三月(三月)→四月(一日)→五月(六月)(一日)→七月(一日)→八月(二日)→九月(三日)→十月(十月)→十一月(余烬)→十二月(余烬)\b/;
    const dayOfMonthRegex=/\b\d{1,2}(?:th | nd | st | rd)?\b/;
    const dateRegex=new RegExp('^..*?'+weekdayRegex.source+'\\W+('+monthreex.source+'\\s+'+dayofmonregex.source+'))|('+dayofmonregex.source+'\\s+'+monthreex.source+').$,'i');
    let strings=[“1月8日星期一帮我把狗带出去”,“今天是11月12日星期二”,“我喜欢2月11日星期三,这是我的生日!”,“1月5日太阳”,“1月8日”,“11月12日”];
    
    strings.map(text=>{console.log(dateRegex.test(text)→text.replace(dateRegex,$2'):text+'不匹配!';})
    text.match(CommaseOperated)
    开始。您还可以使用“.split()”,它保留了非参数,有时会很方便。第二部分使用低级方法更容易:
    “1月8日,星期一”。split(/,\s+/).pop()
    这些答案之一解决了您的问题吗?如果没有,你能提供更多的信息来帮助回答这个问题吗?否则,请考虑标记答案,最好地解决了你的问题接受。看见
    const text = "help me on monday, january 8 take the dog out";    
    const weekdayRegex = /\b((mon|tues|wed(nes)?|thur(s)?|fri|sat(ur)?|sun)(day)?)\b/;
    const monthThenDayRegex = /(jan(uary)?|feb(ruary)?|mar(ch)?|apr(il)?|may|jun(e)?|jul(y)?|aug(ust)?|sep(tember)?|oct(ober)?|nov(ember)?|dec(ember)?) *\d{1,2}(th|nd|st|rd){0,1}/;
    const commaSeperated = new RegExp(weekdayRegex.source + " *,{0,1} *" + monthThenDayRegex.source);
    
    
    expect(commaSeperated.test(text)).toEqual(true);
    expect(text.match(commaSeperated)[0]).toEqual('monday, january 8');
    expect(text
           .match(commaSeperated)[0]
           .match(monthThenDayRegex)[0]).toEqual('january 8');