Question

I am trying to extract a ID from a text using regex. For example 05771292000P from:

<div class="product-price-container">     <a href="/rca-32-class-720p-60hz-led-hdtv-with-built-in-dvd-player-led32b30rqd/226660215" class="product-link subject-price" data-external-product-id="05771292000P">    <span class="save-story-box">   

Tried using the regex (?=id=").*">but it returns ever word after id, which is not helpful.

Any idea what I am doing wrong?

Was it helpful?

Solution

Try

var text = '<div class="product-price-container">     <a href="/rca-32-class-720p-60hz-led-hdtv-with-built-in-dvd-player-led32b30rqd/226660215" class="product-link subject-price" data-external-product-id="05771292000P">    <span class="save-story-box">'
text.match(/data-external-product-id="([0-9A-Za-z]+)"/)[1]

match(...) returns an array with the whole match as the first element and your group match (i.e. [0-9A-Za-z]+) as the second element. If you can trust the source from where you get the text, you can also use jQuery (here the code on jsFiddle):

var text = '<div class="product-price-container">     <a href="/rca-32-class-720p-60hz-led-hdtv-with-built-in-dvd-player-led32b30rqd/226660215" class="product-link subject-price" data-external-product-id="05771292000P">    <span class="save-story-box">'

var id = $($.parseHTML(text)).find("a").attr("data-external-product-id")

alert(id) // 05771292000P

Please have in mind, that everyone can execute malicious JavaScript with $.parseHTML if he has control over the parsed text. So only use the above solution, if you have control over the parsed text.

OTHER TIPS

Try this regex:
id="([0-9A-Za-z]+)"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top