Shouldn't this RegExp work?

后端 未结 4 461
醉酒成梦
醉酒成梦 2021-01-27 02:37
testString = \"something://something/task?type=Checkin\";

patt = new RegExp(\"something\\/(\\w*)\\?\");
match = patt.exec(testString);
document.querySelector(\'#resultR         


        
相关标签:
4条回答
  • 2021-01-27 03:02

    I think this would be enough: (\w*)\?, since / is not captured by \w and the only ? in the string is after your target string.

    0 讨论(0)
  • 2021-01-27 03:12

    You would need to escape the slash in regex literals, and the backslash in string literals which you create regexes from:

    var patt = /something\/(\w*)\?/g;
    // or
    var patt = new RegExp("something/(\\w*)\\?", 'g');
    

    I strongly recommend the first version, it is more readable.

    0 讨论(0)
  • 2021-01-27 03:14

    This is what you need:

    patt = new RegExp(".*/(\\w*)\\?");
    

    http://jsfiddle.net/FJcfd/

    0 讨论(0)
  • 2021-01-27 03:17

    try with this: var pat = /something:\/\/(?:[^\/]+\/)+(\w+)\?(\w+=\w+)/;

    it can match string such as:

    something://something/task?type=Checkin
    something://something/foo/task?type=Checkin
    something://something/foo/bar/task1?type3=Checkin4
    
    0 讨论(0)
提交回复
热议问题