Search code examples
javascriptphppreg-matchmatch

preg_match in Javascript and finding specific variables


I'm trying to match something to the end of a cs go inspect link

steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20S76561198261551396A6723850108D7097851293459665429

Specifically the S76561198261551396A6723850108D7097851293459665429 where the letters S, A, and D are constant.

Added from comments:
I got this /([SM])(\d+)A(\d+)D(\d+)$/ from another source, but honestly I'm just confused about the whole process.

Also, is there an efficient way to get the integers following these letters without just breaking apart the string?


Solution

  • Here is a rather simplified version of what you could do:

    $str = 'steam://rungame/730/76561202255233023/+csgo_econ_action_preview%20S76561198261551396A6723850108D7097851293459665429)';
    $pattern = '/S(\d*)A(\d*)D(\d*)/';
    
    preg_match($pattern, $str, $matches);
    echo '<pre>';
    print_r($matches);
    

    $matches will return this array:

    Array
    (
        [0] => S76561198261551396A6723850108D7097851293459665429
        [1] => 76561198261551396
        [2] => 6723850108
        [3] => 7097851293459665429
    )
    

    The last three elements of the array are the numbers you're looking for.