upufo
V2EX  ›  问与答

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

  •  
  •   upufo · Mar 21, 2016 · 1902 views
    This topic created in 3882 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
    TTry
    Mar 21, 2016❤️1
    1
    str.match(regexp)[1]
    ppgs8903
    ppgs8903
    Mar 21, 2016❤️1
    2
    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   ·   Privacy   ·   Solana   ·   3696 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 05:21 · PVG 13:21 · LAX 21:21 · JFK 00:21
    ♥ Do have faith in what you're doing.