/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] For Regular Expressions Experts Only

Given a string such as the following:

var str = ‘<DIV onmouseup=”return endDragElement(event)” class=child2 onmousedown=”return setDragElement(this, event)” id=childDIV2><B>DIV2</B></DIV>
<DIV onmouseup=”return endDragElement(event)” class=child1 onmousedown=”return setDragElement(this, event)” id=childDIV1><B>DIV1</B></DIV>’;

What Regular Expression will remove the in-line events from this?

Thanks.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceauthorApr 04.2006 — Well, I seem to have accomplsihed this on my own. Is the following the best way to do it?

str = str.replace(/ onmousedown="[^"]*"/gi, '');

str = str.replace(/ onmouseup="[^"]*
"/gi, '');

EDIT: And I found this single statement will do both:

str = str.replace(/ onmouse[^=]*="[^"]*"/gi, '');

But, still, is this the best way?
Copy linkTweet thisAlerts:
@Tweak4Apr 04.2006 — That's about the only way I can think of... The regex doesn't care that you're looking for event handlers or anything else- it just sees text. To that end, you'd have to code every handler individually.

In this case, since they are both mouse events, there is no reason not to combine them into a single regex, so this looks like the way to go.

If you can't guarantee that you'll only need to strip mouse-based events, you could always change it to:

str = str.replace(/ on[^=]*="[^"]*"/gi, '');

This would strip any event handler that starts with "on" (onClick, onBlur, onKeyPress, etc), but the more you generalize your regex, the more you open yourself up to false positives, so if you're reasonably sure that the mouse events are all you're encounter, then it looks like you've got it already.
Copy linkTweet thisAlerts:
@phpnoviceauthorApr 04.2006 — I was basically looking to see if there was a better way to select all characters between two delimiters (double-quotes, in this case) -- without worrying about greedy matching (if that is supported by JavaScript Regular Expressions). If not, I'm happy. ?
Copy linkTweet thisAlerts:
@phpnoviceauthorApr 04.2006 — Silence is golden, I guess. ?
×

Success!

Help @phpnovice 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.19,
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,
)...