Search code examples
javascripthtmlregexstring-matching

regex match everything between two different characters


I want to match a string between (but not including) these two characters: ? and &

Example string:

localhost/path/doc.html?970441179&token=specialtoken&actionurl=/portletaction/01654/0112

So from the above I want to match the string 970441179


Solution

  • var str = "?samplestring&";
    var patt = /[?]([^&]*)[&]/g;
    var res = patt.exec(str)[1];
    

    'res' is your desired result.