-4

I have HTML code like this:

<div class="inside">
<h2>Some text</h2>
Lorem Ipsum:<br />
  [423A18K5][R68A18K5]       
</div>

<div class="inside2">
<h2>Some other text</h2>
Lorem Ipsum:<br />
  [9903A1K5][P095GR31]       
</div>

What i need to do: i must select all codes between [ and ] with jQuery (and to pass them to ajax later). NOTE: Instead of [] i can use some other symbols, but i cant use HTML tags.

Do you have any sugestions?

SomeoneS
  • 1,207
  • 2
  • 19
  • 34
  • 2
    [What have you tried?](http://www.whathaveyoutried.com) – James Montagne Sep 13 '12 at 19:09
  • 1
    you can use regex to do this, check this: http://stackoverflow.com/questions/1454913/regular-expression-to-find-a-string-included-between-two-characters-while-exclu – Ricardo Binns Sep 13 '12 at 19:09
  • 2
    Why not just output span tags around these elements with a class name that lets you easily select them? If you are trying to build something with jQuery AJAX type functionality, why not change up the HTML source a bit to make it work more efficiently and effectively rather than having to sort through the entire body of the document with regex? – Mike Brant Sep 13 '12 at 19:10
  • @MikeBrant i cant put HTML tags at all, i write that in question. Reason: sanitisation system that i cant change (yes, this html is called from database) – SomeoneS Sep 13 '12 at 19:17
  • @JamesMontagne is it really important? I an not familiar with regex, and i tried some Jquery tricks, but that was total mistake. – SomeoneS Sep 13 '12 at 19:18
  • If you can't change the output HTML source, then how will you put the necessary javascript into the page? – Mike Brant Sep 13 '12 at 19:19
  • @SomeoneS It actually is pretty important, as you can see by all of the downvotes on your question. The community generally expects to see some indication that you have tried prior to asking the question. You will find much better responses when your questions include code which you have tried which is not working correctly. – James Montagne Sep 13 '12 at 19:21
  • @MikeBrant i can change header, but i cant change code i give in question becouse that code is from database. – SomeoneS Sep 13 '12 at 19:21
  • @James Montagne well, sorry, i didnt know, i tought that it is stupid to put totaly dumb and useless code (yeah, my tries was extremly dumb). – SomeoneS Sep 13 '12 at 19:22
  • @SomeoneS OK. Then you are using some sort of server-side scripting to do this. Why not just modify the string value returned from the database with your server-sdie script to add tags? That might be better than trying to force a javascript regex match onto the end client. You could also probably easily cache your modified html. To prevent needing to do regex searches over and over on the same content. – Mike Brant Sep 13 '12 at 19:27

1 Answers1

1

Not sure if all of the divs you're trying to grab the data from has a common class or if they're all unique as in your example. Also you give a very specific format for the contents of the div above. I.e.:

<div class="..."> ... [#s][#s]...</div>

The code below works assuming that what you grab is in the same format. Basically it selects the div you want, gets the text inside, then drops leading or ending spaces, drops anything before the first '[', then gets rid of the first [ and last ], then creates the array, splitting on ']['.

    var codes = $('.inside').text().trim().replace(/^[^\[]*/,'').replace(/^\[/,'').replace(/\]$/,'').split('][');

The regex could probably be cleaned up a bit, but it's simple and it works.

Peter Oram
  • 6,213
  • 2
  • 27
  • 40