Search code examples
javascriptregexgon

Regex help to get a variable value from Javascript block


I have a JavaScript block from where I need to extract a value. it look something like this:

<script type="text/javascript">
//<![CDATA[
window.gon={};gon.session_id="1a2b3c4d5e";gon.user={"id":"5a4b3c2d1e"}
//]]>
</script>

I need to get value for id. I tried with multiple options but doesn't seems to be working. E.g. script type="text/javascript" gon.user.id="(.+?)"

PS: gon is gem used to pass dynamic variables in Javascript block.


Solution

  • The regex will be

    /"id":"(\d*\w*)"/i
    

    The above pattern will search for the value in "id":"5a4b3c2d1e" pattern and will return 5a4b3c2d1e.

    https://regex101.com/ is an awesome site to play around with Regex.