代码之家  ›  专栏  ›  技术社区  ›  Chris Barry

用于在两组字符之间提取文本的正则表达式(Javascript)

  •  1
  • Chris Barry  · 技术社区  · 16 年前

    我想用Javascript在字符串的两点之间提取一些文本

    假设字符串是

    "start-extractThis-234"
    

    结尾的数字可以是任何数字,但连字符始终存在。

    extractThis
    
    3 回复  |  直到 16 年前
        1
  •  3
  •   mkoryak    16 年前

    为什么不直接做呢

    var toExtract = "start-extractThis-234";
    var extracted = null;
    var split = toExtract.split("-");
    if(split.length === 3){
       extracted = split[1];
    }
    
        2
  •  4
  •   Lobos    13 年前
    string = "start-extractThis-234"
    
    console.log( string.match( '-(.*)-' )[1] );
    
    //returns extractThis
    
        3
  •  0
  •   SilentGhost    16 年前
    ^.+?-(.+?)-\d+$