/    Sign up×
Community /Pin to ProfileBookmark

RegExp Matching

Hello, I’ve been having trouble with RegExp in JavaScript.

I have a string in one of my scripts that is similar to this:

[CODE]var str = ‘
<div class=”main” id=”content”></div>
<div class=”sub” name=”subcontent”></div>’;[/CODE]

How do I return an array of the values “main” and “sub”, or anything else in the class tags?

I came up with something like this, but it fails to return the values I’m looking for.

[CODE]alert(str.match(/(?=class=”)w+(?=”)/gi));[/CODE]

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@MalgrimJul 21.2009 — Maybe you can work with that
var str = '&lt;div class="main" id="content"&gt;&lt;/div&gt;&lt;div class='sub' name='subcontent'&gt;&lt;/div&gt;';

var pattern = /&lt;w+(?:s+(?:w+)s*=s*(?:"(?:(?:[^"]+|[^\]\")+)"|'(?:(?:[^']+|[^\]\')+)'))*s*/?&gt;/g;
var subpattern = /(w+)s*=s*(?:"((?:[^"]+|[^\]\")+)"|'((?:[^']+|[^\]\')+)')/g;
var pmatch;
var submatch;
var i=0;
while (pmatch = pattern.exec(str)) {
i++;
var j=0;
while (submatch = subpattern.exec(pmatch)) {
j++;
if (submatch[2]) {
document.write('element '+i+' attribute '+j+': '+submatch[1]+' = '+submatch[2]);
document.write('&lt;br /&gt;');
} else if (submatch[3]) {
document.write('element '+i+' attribute '+j+': '+submatch[1]+' = '+submatch[3]);
document.write('&lt;br /&gt;');
}
}
}


but if you could explain what you intend in more detail, I think it would be much better to work with the DOMParser.
Copy linkTweet thisAlerts:
@VorticoauthorJul 22.2009 — Wow, that code does a lot more than I'm looking for.

Basically I'm trying to search a string for some text, then return what comes after it until it finds another string. In other words, search for...
[CODE][COLOR="red"]foo[/COLOR][COLOR="SeaGreen"]*[/COLOR][COLOR="Red"]bar[/COLOR][/CODE]
...in this string...
[CODE]var str = "[COLOR="Red"]foo[COLOR="SeaGreen"]7[/COLOR]bar foo[COLOR="SeaGreen"]8[/COLOR]bar foo[COLOR="SeaGreen"] rhu[/COLOR]barb[/COLOR]";[/CODE]
...and return "7", "8", " rhu" in an array.

It sounds like it could be done with Regular Expressions, but any method will be fine.

Thanks.
Copy linkTweet thisAlerts:
@web_bertJul 22.2009 — try this:

/foo(.*?)bar/gi
Copy linkTweet thisAlerts:
@VorticoauthorJul 22.2009 — Thanks! That's [I]almost[/I] all the info I need. Now how do I make it an array with all three values? Is it close to this?
[CODE]var pattern = /foo(.*?)bar/gi;
alert(pattern.exec(str)[1]);[/CODE]
Copy linkTweet thisAlerts:
@VorticoauthorJul 23.2009 — Got it! ?

[CODE]var str = "foo7bar foo8bar foo rhubarb";
var pattern = /foo(.*?)bar/gi;
while (strMatch = pattern.exec(str)) {
document.write(strMatch[1]);
}[/CODE]
×

Success!

Help @Vortico 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.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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