The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 18, 2019, 12:37:43 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: How to grab the title line for a drop down when the values are all numbers.  (Read 4172 times)
tommytx
Expert
****
Offline Offline

Posts: 123


View Profile WWW
« on: May 23, 2010, 11:56:44 AM »

This thing just is not cooperating with me.. very much... though its Regex, its the javascript flavor.. hope this is in the right place and not supposed to be in javascript... its really regex.. with JS flavor..
It has to run javascript... due to its use...


<script type="text/javascript" charset="utf-8">

var username = '<OPTION value=1>Mar 10 (100)</OPTION> <OPTION value=2 selected>Apr 10 (100)</OPTION> <OPTION value=3>May 10 (100)</OPTION> <OPTION value=4>Jun 10 (100)</OPTION> <OPTION value=5>Jun 10 (100)</OPTION>'

display the results:
alert(/(?<=ected value=\(d{1,2}>)\w+\s\d+\s\(\d+\)/i.test(username));

save the result:
mydat = /(?<=ected value=\(d{1,2}>)\w+\s\d+\s\(\d+\)/i.test(username)

</script>

In this case the results should be -> Apr 10 (100)


When an option is selected it looks like this....
*************************************************
<OPTION value=2 selected>Apr 10 (100)</OPTION>

Now I need to extract the actual value not the number 2...
**********************************************************
Apr 10 (100)


So bottom line I want to grab the selected data based on the number selected....
So I need everthing between the selected  <option> and the </option>
The selection between the options could be anything... not just the pattern shown above...

Any assistance or advice would be greatly appreciated... I can use the dom to extract the value with no problem, but cannot get the actual name using the DOM that I know..

Logged
kurdt
Lifer
*****
Offline Offline

Posts: 1153


paha arkkitehti


View Profile
« Reply #1 on: May 23, 2010, 12:11:41 PM »

You could use jQuery and use $('select').text() which gives the current selected text. $('select').val() gives you the value.
Logged

I met god and he had nothing to say to me.
tommytx
Expert
****
Offline Offline

Posts: 123


View Profile WWW
« Reply #2 on: May 23, 2010, 12:19:21 PM »

this is running inside UBOT if you have ever heard of that and it is very restrictive... it just knows basic javascript so any normal javascript regex should be ok... but i am sure it does not understand jqurey..which is much more complex.
But thanks anyway..
I suppose I could play with str.split and then search for the select and then grab associated stuff with the str.match stuff.. but not too good with that either.
Logged
tommytx
Expert
****
Offline Offline

Posts: 123


View Profile WWW
« Reply #3 on: May 23, 2010, 12:26:17 PM »

I am not familiar with jquery, but dom gave the value=2 just fine... which is 2 but the value is not what i want. its the data that shows in the window.... <OPTION value=2 selected>Apr 10 (100)</OPTION> as you can see here the value=2 which is easy to extract with DOM but I need the actual data displayed in the window of the drop down.. and not the actual value..

Either way UBOT cannot speak Jquery...
selected text. $('select').val()
Logged
kurdt
Lifer
*****
Offline Offline

Posts: 1153


paha arkkitehti


View Profile
« Reply #4 on: May 23, 2010, 12:55:59 PM »

Code:
var select = document.getElementById('id')
var text = select.textContent
Basically the value is now assigned into text variable. Do whatever you want with it Smiley
Logged

I met god and he had nothing to say to me.
tommytx
Expert
****
Offline Offline

Posts: 123


View Profile WWW
« Reply #5 on: May 23, 2010, 01:28:36 PM »

outerTEXT = Mar 10 (100) Apr 10 (100) May 10 (100) Jun 10 (100) Jun 10 (100)

innerHTML  =  <OPTION value=1 selected>Mar 10 (100)</OPTION> <OPTION value=2>Apr 10 (100)</OPTION> <OPTION value=3>May 10 (100)</OPTION> <OPTION value=4>Jun 10 (100)</OPTION> <OPTION value=5>Jun 10 (100)</OPTION>

outerHTML = <SELECT> <OPTION value=1 selected>Mar 10 (100)</OPTION> <OPTION value=2>Apr 10 (100)</OPTION> <OPTION value=3>May 10 (100)</OPTION> <OPTION value=4>Jun 10 (100)</OPTION> <OPTION value=5>Jun 10 (100)</OPTION></SELECT>

As you can see grab text will not help as there is no selected text, just all the text..
That is why I think the only way is to figure out what RegEx is needed to jerk out the "May 10 (100)" which is pointed to by the selected item.. 3

Both the innerHTML and outer HTML has the selection pointer but the basic content does not mark the selected one.
That is why  I may have to take the value=1 and use split on the innerTEXT and count down to the third one in Javascript... but I was hoping someone could mod my REGEX above to pull out that simple little ""May 10 (100)"  base on the select statment pointing to it..
Logged
Bompa
Administrator
Lifer
*****
Offline Offline

Posts: 564


Where does this show?


View Profile
« Reply #6 on: May 23, 2010, 05:33:54 PM »

You said twice that this is a regex problem, so I will take that approach.

Is this not working?
alert(/(?<=ected value=\(d{1,2}>)\w+\s\d+\s\(\d+\)/i.test(username));

I do not understand the ?<=ected

In order to capture the value, I would have:  value=(\d+?)>
That is any number of digits up to the first >

Following that, you need to capture the date like:  Mar 10 (100)<

For that I would try a class containing alphanumerics, parentheses', and a space

 [\w() ]


I would quantify that class with a + and use the ? to make it not greedy; "stop
at the first" left angle bracket

([\w() ]+?)<


It looks like what you had there would work, but hard for me to read.

Bompa

Logged

"The most beautiful and profound emotion we can experience is the sensation of the mystical..." - Albert Einstein
tommytx
Expert
****
Offline Offline

Posts: 123


View Profile WWW
« Reply #7 on: May 23, 2010, 05:46:32 PM »

I do not understand the ?<=ected

selected>Mar 10 (100)</OPTION>

I think  I am missing a couple of characters for the start are which is "selected"

I might be making it too hard, all I really want to do each time is find the word "selected" then pull all from the word selected to the </OPTION> then simply drop the selected on the left and the option on the right that leaves me with "Mar 10 (100)" and I am done... is that simple with regex... maybe I am making it too complicated..

selected>Mar 10 (100)</OPTION>

selected>
Mar 10 (100)
</OPTION>

Logged
kurdt
Lifer
*****
Offline Offline

Posts: 1153


paha arkkitehti


View Profile
« Reply #8 on: May 23, 2010, 10:36:09 PM »

outerTEXT = Mar 10 (100) Apr 10 (100) May 10 (100) Jun 10 (100) Jun 10 (100)

innerHTML  =  <OPTION value=1 selected>Mar 10 (100)</OPTION> <OPTION value=2>Apr 10 (100)</OPTION> <OPTION value=3>May 10 (100)</OPTION> <OPTION value=4>Jun 10 (100)</OPTION> <OPTION value=5>Jun 10 (100)</OPTION>

outerHTML = <SELECT> <OPTION value=1 selected>Mar 10 (100)</OPTION> <OPTION value=2>Apr 10 (100)</OPTION> <OPTION value=3>May 10 (100)</OPTION> <OPTION value=4>Jun 10 (100)</OPTION> <OPTION value=5>Jun 10 (100)</OPTION></SELECT>

As you can see grab text will not help as there is no selected text, just all the text..
That is why I think the only way is to figure out what RegEx is needed to jerk out the "May 10 (100)" which is pointed to by the selected item.. 3

Both the innerHTML and outer HTML has the selection pointer but the basic content does not mark the selected one.
That is why  I may have to take the value=1 and use split on the innerTEXT and count down to the third one in Javascript... but I was hoping someone could mod my REGEX above to pull out that simple little ""May 10 (100)"  base on the select statment pointing to it..
Going with regex is horrible approach because there's so many gotchas. What do you mean "there's no selected text"? I don't have time now to try with code but I did multi-option select (which I didn't the last time which explains why I got the text with textContent), and when I look at the DOM, there's a DOM value "selected" in select element's children which gives false or true. So you could grab the select and loop thru all children looking for children where selected is true and then extract text value?

I'm not trying to be pick fun of you but are you familiar with DOM traversing or do you just know that there's a thing called DOM? For a long time I didn't understand what DOM really was and how it works. Because when it comes to value extraction, DOM is much better than regex but it's also harder to understand unless you play with javascript a lot. Javascript kinda forces you to learn what DOM really is and how it works. Nowadays I don't use regex at all when scraping, DOM gives me everything faster and easier. If you can see it in your browser, you can find it in DOM.
Logged

I met god and he had nothing to say to me.
tommytx
Expert
****
Offline Offline

Posts: 123


View Profile WWW
« Reply #9 on: May 24, 2010, 12:54:06 PM »

Thanks for all the suggestions Kurdt, but I am limited in what I can do when working inside a specific tool.. you of course are limited to its capability.. and I doubt that you are familiar with UBOT, but its a super easy and quick web scraper that can be programmed to quickly return just about anything.. but apparently it has a hang up with a drop down... It can see the value easily.. but the names of each value is only seen as one long line like below..
Now it does have a javascript module that I can send the scrped code to grab what I need... and since regex does not seem to be simple enought maybe I will just use the simple javascript indexof( to find the word selected, then the next end option which would be "selected>Mar 10 (100)</OPTION>" then I simply tell javascript to drop the word selected and drop the word </OPTION> and bing I have my answer every time..

<OPTION value=1 selected>Mar 10 (100)</OPTION> <OPTION value=2>Apr 10 (100)</OPTION>

I just thought there was a simple regex expression to strip out "Mar 10 (100)" in a blink of the eye as it takes me 5 or 6 lines of javascript code to rip it out and one simple line of regex should do the same thing... am I wrong.

To tell you the truth.. I cannot understand why ubot cannot do this... its relatively simple,, but it won't so I need a work around....  It could be that I am just not explaining what I need correctly... simply strip out the data in every case no matter what it is that will always and forever be located between the word select and the next </OPTION>

Seems pretty clear to me... but might not be clear to others..

Logged
tommytx
Expert
****
Offline Offline

Posts: 123


View Profile WWW
« Reply #10 on: May 24, 2010, 01:03:16 PM »

Bompa probably gave me the answer above, but I am as dumb as a rock with regex and was not able to put all the pieces together to make it work.. I am so dumb that I need the set up shown to me like:
<script>
regex = something
replacewith=something
answer=work.replace(regex, replaceWith);
</script>
Not a real example, but just to show what would help me...
I want to input that long outerHTML jibberish with all the options and whip a regex forumla on it and have the answere come out as "Mar 10 (100)"

Hope all this make sense...


Logged
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!