upufo
V2EX  ›  问与答

javascript 的正则表达式 不支持优先取分组里面的值 有什么解决办法吗

  •  
  •   upufo · Mar 21, 2016 · 1820 views
    This topic created in 3789 days ago, the information mentioned may be changed or developed.
    正则: hello src=(.+) yes
    内容: hhsdj dskj hello src=value yes

    上面的正则只能匹配出 [ hello src=value yes ]
    如果我想要取等号后面的 value 这个字符串 应该怎么写呢?
    2 replies    2016-03-21 09:07:30 +08:00
    TTry
        1
    TTry  
       Mar 21, 2016   ❤️ 1
    str.match(regexp)[1]
    ppgs8903
        2
    ppgs8903  
       Mar 21, 2016   ❤️ 1
    var re = /hello src=(.+) yes/;
    var str = "hhsdj dskj hello src=value yes";
    re.exec(str);
    alert(RegExp.$1);
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5309 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 09:03 · PVG 17:03 · LAX 02:03 · JFK 05:03
    ♥ Do have faith in what you're doing.