-1

I would like to pull a token out of an HTML form with Regex. Every time the page loads, the token changes.

<input type="hidden" name="lt" value="LT-24-EskCKFJ4NghflvpXkuveCdZ1VvWPLi" />

I would like to isolate the value of this hidden input.

I found http://regexr.com/ and I have made some progress. My current regex is "([a-zA-Z0-9\-]*)?" which isolates everything within quotation marks.

I am using Java. The entire HTML page is given to me in a string after a GET request to the server. I need the LT token to re post.

Is there a way to isolate the LT token specifically?

Tyler Fricks
  • 91
  • 1
  • 8
  • 1
    Please tag your question with the tool you're using the regular expression in. Regex is an inappropriate tool for this sort of thing, but might be useful if you're using a text editor and just need a quick-and-dirty solution. – zzzzBov Jan 28 '16 at 18:46
  • Please provide more detail as the context of the question is unclear. For example, this JS would seem a much more direct solution: var token = document.getElementsByName("lt")[0].value; – Yogi Jan 28 '16 at 18:54
  • The HTML page is returned as a string in Java after a HTTP_GET request is submitted. I need the token for a returning HTTP_POST request. – Tyler Fricks Jan 28 '16 at 20:56
  • Your question is answered by the best post on SO: http://stackoverflow.com/a/1732454/8681 – Jorn Jan 28 '16 at 21:42
  • @Jorn Thank you for the comment. Funny. – Tyler Fricks Jan 28 '16 at 22:16

1 Answers1

0

To make sure you get the right element, even if the HTML is slightly different, you can use this regex. (No lookbehinds because we're using JS.)

\sname="lt"\s+(?:.*?\s+)?value="([^"]+)"

It ensures that even when there's something between the name attribute and the value attribute, you still match. It also keeps into account that there might be multiple space separators. However, the name attribute has to come before the value attribute to work.

Example here.

var string = '<input type="hidden" name="lt" value="LT-24-EskCKFJ4NghflvpXkuveCdZ1VvWPLi" />',
    text = /\sname="lt"\s+(?:.*?\s+)?value="([^"]+)"/.exec(string)[1];

document.getElementsByTagName("p")[0].innerHTML = text;
Bram Vanroy
  • 27,032
  • 24
  • 137
  • 239
  • What makes you sure OP wants to use JS? And if so, wouldn't `document.getElementsByName("lt")[0].value` be far better? – Jan Jan 28 '16 at 19:06
  • @Jan I thought he wanted to use JS because he is talking about *when the page loads* so I'm guessing he's talking about a website, more specifically at the client-side. You are right in pointing out that if he wanted to get the value of an HTML element (probably true) your approach would be best. I simply gave a hint in the direction on how to use regex to get the string he wanted. – Bram Vanroy Jan 28 '16 at 19:47