0

I have a string as follows or it can be anything similar to this:

http://localhost:3003/?sp-tk=A8FEE0A4AFD2A086277CC79449AD69E5D34734455900AEED7C0A7C77EC580187D9D2FE1B286F7B5989B421B27E6FE2D1CAD2CCEB4372A80FFF6DC5D1AC6E246DB3BBAD7EB8DD7DAD1C5ED79A2114F0E3A036E898287021ABEFE642F74FAE5372E6525E0C54732B7EA9691F84C27EEB6AE60029B7613B68DA8DA3AE69887F6E815EA0A3415F08C827AF21DBFB82AE7247B297F8CFAD0DD3F7D0ED81FC095375F6242CED940B2B55D8707BCB1D85E54CBB98E83CB8

What I need is the section after sp-tk:

A8FEE0A4AFD2A086277CC79449AD69E5D34734455900AEED7C0A7C77EC580187D9D2FE1B286F7B5989B421B27E6FE2D1CAD2CCEB4372A80FFF6DC5D1AC6E246DB3BBAD7EB8DD7DAD1C5ED79A2114F0E3A036E898287021ABEFE642F74FAE5372E6525E0C54732B7EA9691F84C27EEB6AE60029B7613B68DA8DA3AE69887F6E815EA0A3415F08C827AF21DBFB82AE7247B297F8CFAD0DD3F7D0ED81FC095375F6242CED940B2B55D8707BCB1D85E54CBB98E83CB8

To do so I found a code as follows:

function getTokenFromUrl() {
    name="sp-tk";
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/,
        "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(
            /\+/g, " "));
}

and it works. But I do not understand the regular expression parts. Can anyone shed a light on that and also if there is a simpler way and maybe cleaner achieving that?

Samuil Petrov
  • 542
  • 1
  • 13
  • 24
Hamed Minaee
  • 2,480
  • 4
  • 35
  • 63
  • 1
    The regex is extracting a param from query string, you can do a simple split by "sp-tk=" and get the right part but if you have additional params in the query string this will fail to get correct data. – Samuil Petrov Aug 11 '17 at 12:01
  • 1
    [How can I get query string values in JavaScript?](https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – Alex K. Aug 11 '17 at 12:04
  • Possible duplicate of [How can I get query string values in JavaScript?](https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – thexpand Aug 11 '17 at 12:09

1 Answers1

2

You can use this code (i think this regex easier to read):

    var myString = 'http://localhost:3003/?sp-tk=A8FEE0A4AFD2A086277CC79449AD69E5D34734455900AEED7C0A7C77EC580187D9D2FE1B286F7B5989B421B27E6FE2D1CAD2CCEB4372A80FFF6DC5D1AC6E246DB3BBAD7EB8DD7DAD1C5ED79A2114F0E3A036E898287021ABEFE642F74FAE5372E6525E0C54732B7EA9691F84C27EEB6AE60029B7613B68DA8DA3AE69887F6E815EA0A3415F08C827AF21DBFB82AE7247B297F8CFAD0DD3F7D0ED81FC095375F6242CED940B2B55D8707BCB1D85E54CBB98E83CB8'

    var myRegexp = /sp-tk=(.*?)(&|$)/;
    var match = myRegexp.exec(myString);
    var spTk = match[1]
    console.log(spTk);

Regex explanation

  • sp-tk= => Find a string that starts with sp-tk=
  • (.*?) => The string should be followed by any characters (the ? stand for non-greedy mode) and sourounded with parenthesis because we want to extract the match of this part.
  • &|$ Since we are in non-greedy mode, we have to say on which characters the .*? should stop.

Then you get the match at index 1 because 0 is the whole match (including sp-tk)


Note that this regex will work event if there are other url params before or after sp-tk.

Anthony Raymond
  • 7,434
  • 6
  • 42
  • 59
  • Can you explain this part more: Get the string after sp-tk= and until & or end of line occurs. – Hamed Minaee Aug 11 '17 at 12:14
  • Thanks still I do not get this part: &|$ : Since we are in non-greedy mode, we have to say on which characters the .*? should stop. – Hamed Minaee Aug 11 '17 at 12:23
  • You want to match all the caracters until you encounter `&` (which means that another parameters is appended to the url), or until you encounter `$` (which in regex language stands for `end of line`). This is needed because i used `.*?` and this expression need to know until when it should match, if i had used `.*` instead it will have matched until the end of the line automatically. – Anthony Raymond Aug 11 '17 at 12:27