0

I need to retrieve 71drwec4 , 51drdsf3 from the below expression or any other characters other than 71drwec3 between "a_secret"> and </div> or something similar to that:

<div class="fs" id="a_secret">71drwec4</div>

<div class="fs" id="a_secre">51drdsf3</div>

<div class="fs" id="a_secr">54451drwec3</div>
Grimthorr
  • 6,856
  • 5
  • 41
  • 53
vishnu Simbu
  • 43
  • 1
  • 4
  • 10

4 Answers4

1

Do you have to use a regex for this? if the text to parse is say, HTML and you can use jQuery, you can use the .text() method here to retrieve the content from with an ID.

E.g.

var whatIwant = $("#a_secret").text();

http://api.jquery.com/text/

Cristian Sanchez
  • 31,171
  • 11
  • 57
  • 63
dougajmcdonald
  • 19,231
  • 12
  • 56
  • 89
  • Its possible by jquert..but i need regular expression.thank you – vishnu Simbu Mar 30 '12 at 21:38
  • 4
    what's the reason for regex? can you give a bit more background as to what you need to do? perhaps there is a simpler route. – dougajmcdonald Mar 30 '12 at 21:39
  • Because i am retrieving another page source using http get request..i am saving that whole page source as an string.and with that string i am finding that particular value. – vishnu Simbu Mar 30 '12 at 21:41
  • I still like this answer the best. Even if you are getting the page over http get request, you can parse the html in the resulting string and use jQuery (or even just regular dom manipulations). See http://stackoverflow.com/questions/888875/how-to-parse-html-from-javascript-in-firefox – Cameron Mar 30 '12 at 22:03
  • Is there are need to save the page? or can you get away with simply parsing the HTML on the fly? reason I ask is that you can again use jQuery with methods such as .load() to target a specific area of a page you're making a request to and perform an action (in this case loading into a DOM node on your own page) based on the result – dougajmcdonald Mar 31 '12 at 08:31
1

how about this:

var pattern = /<div class="fs" id=".*?">(.*?)<\/div>/gm;
var src = '<div class="fs" id="a_secret">71drwec4</div> \n <div class="fs" id="a_secre">51drdsf3</div> \n <div class="fs" id="a_secr">54451drwec3</div>';
var match;
while (match = pattern.exec(src)) {
  alert(match[1]);
}

Change it as you need it (I don't know what your id's will look like or if they all have the same class, etc). If you are looking to match elements from inside your own page, jQuery would be way easier, as other posters have mentioned.

And... obligatory reference to other SO post: RegEx match open tags except XHTML self-contained tags

Community
  • 1
  • 1
Cameron
  • 1,675
  • 11
  • 12
0
// Match against your HTML, store matches in new 'matches' Array
var matches = htmlStr.replace(/<div.*?>(.+?)<\/div>/g, "$1 ").trim().split(' ');

...this assumes there are no spaces in the values you're trying to retrieve, you'd need to tweak it a bit if spaces are valid. But against your provided test HTML:

<div class="fs" id="a_secret">71drwec4</div>
<div class="fs" id="a_secre">51drdsf3</div>
<div class="fs" id="a_secr">54451drwec3</div>

...this approach yields:

["71drwec4", "51drdsf3", "54451drwec3"]

Cheers

Madbreaks
  • 19,094
  • 7
  • 58
  • 72
0

One of these might work with javascript.

overrun protected with <name>

str = '
<div
  (?=\s) 
  (?= (?:[^>"\']|"[^"]*"|\'[^\']*\')*? (?<=\s) id \s*=
      (?: (?>              \s* ([\'"]) \s* a_secret \s* \g{-1} )
        | (?> (?!\s*[\'"]) \s*             a_secret (?=\s|>) )   
      )
  )
  (?> \s+ (?:".*?"|\'.*?\'|[^>]*?)+ 
>
  ) (?<! /> )

(?<not_71drwec3>(?:(?!71drwec3).)*?) </div\s*>
';

unprotected with <name>

str = '
<div
  (?=\s) 
  (?= (?:[^>"\']|"[^"]*"|\'[^\']*\')*? (?<=\s) id \s*=
      (?:               \s* ([\'"]) \s* a_secret \s* \g{-1}
        |  (?!\s*[\'"]) \s*             a_secret (?=\s|>) 
      )
  )
  \s+ (?:".*?"|\'.*?\'|[^>]*?)+ 
>
  (?<! /> )      // line is worthless when unprotected

(?<not_71drwec3>(?:(?!71drwec3).)*?) </div\s*>
';

unprotected no <name> without \g{} notation

str = '
<div
  (?=\s) 
  (?= (?:[^>"\']|"[^"]*"|\'[^\']*\')*? (?<=\s) id \s*=
      (?:               \s* ([\'"]) \s* a_secret \s* \1    // Group 1
        |  (?!\s*[\'"]) \s*             a_secret (?=\s|>) 
      )
  )
  \s+ (?:".*?"|\'.*?\'|[^>]*?)+ 
>
  (?<! /> )      // line is worthless when unprotected

((?:(?!71drwec3).)*?) </div\s*>                           // Group 2
';