/    Sign up×
Community /Pin to ProfileBookmark

Help me understand the Match method..

Hi guys, so I’m just having a hard time understanding the Match() method of Javascript..

I’m reviewing the codes made by greasemonkey developers; I found that that’s the best way to learn javascript because everything is hand coded, no jquery no libraries involved.

Here’s the code that I don’t understand.. I have basic knowledge okay.. Just help me understand this. prolly give me some explainations.. I’ll stick bits of codes..

Here, the result is an array.. So I can assume that a match operation returns an array.

Here’s the value of the header: “RGHCo”, Level 15 Tycoon
What is result[1]? and what is result[0]? Is result[2] available?

[CODE]header = document.getElementById(‘app8743457343_content’);
header = header.getElementsByTagName(‘h1’)[0].innerHTML;
var result = header.match(/”(.*)”, Level [0-9]+ (w+)/);
this.name = result[1];
this.type = result[2];
[/CODE]

Here’s another code that I don’t understand:
In this code, you can see that we have delay variable. The delay variable holds a paragraph that have a jail string.

Then in the if statement, we can see that we changed the value of delay to parseFloat(delay[1]) + 0.01.. Now can you please tell me the value of delay[1]? If we parseFloat(delay) that will return NaN right? But we can see that the developer used the value of delay[1] then add 0.01 to it.

[CODE]divs = header.getElementsByTagName(‘p’);
for (var i = 0; i < divs.length; i++) {
var delay = divs[i].innerHTML.match(/jail in ([0-9.]+) hour/);
if (delay) {
delay = parseFloat(delay[1]) + 0.01;
this.jail_delay = Page.now + Math.floor(3600 * delay);
break;
}[/CODE]

Please help me understand this..

Thanks!!

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@MrNobodyJan 18.2009 — Here's the value of the header: "RGHCo", Level 15 Tycoon

What is result[1]? and what is result[0]? Is result[2] available?[/QUOTE]

Learn to experiment. Try executing this:
[code=html]<script type="text/javascript">
header = '"RGHCo", Level 15 Tycoon';
var result = header.match(/"(.*)", Level [0-9]+ (w+)/);
alert("| " + result.join(" | ") + " |");
</script>[/code]
Copy linkTweet thisAlerts:
@alcatraz678authorJan 19.2009 — MrNobody

Thanks, I think I got it..
×

Success!

Help @alcatraz678 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...