代码之家  ›  专栏  ›  技术社区  ›  Mosh Feu Alex Chen

pharse内部的正则表达式-Javascript

  •  0
  • Mosh Feu Alex Chen  · 技术社区  · 12 年前

    我有这样的表达:

    jQuery("#SomeId")
    

    我正在使用

    jQuery\(\".*?\"\)
    

    实际结果:

    jQuery(“#SomeId”)
    

    预期结果:

    #SomeId
    

    我该怎么做?

    2 回复  |  直到 12 年前
        1
  •  1
  •   nanobash Anthony Chu    12 年前

    尝试:

    var str = 'jQuery("#SomeId")';
    
    var reg = /#(\w+)/;
    
    var ret = reg.exec(str); // ["#SomeId", "SomeId"]
    

    如果您想要结果 # 使用 ret[0] 否则 ret[1]

    console.log(ret[0]); // #SomeId
    console.log(ret[1]); // SomeId
    
        2
  •  1
  •   Toto    12 年前

    将正则表达式更改为:

    jQuery\(\"(.*?)\"\)
    

    #SomeId 将在第1组。