Search code examples
javascripthtmlregexk6

Parsing only the id with regex


Using this regex /var userId = (\d+)/, I can find the id of the user. Except it returns "var userId = 117051" instead of just 117051. I looked around in regexr but I'm not very good with regex. Is there a way to only get the id?


Solution

  • string = "some string containing var userId = 117051";
    var foundId = string.match(/var userId = (\d+)/)[1];
    console.log(foundId); // "117051"