Link to home
Start Free TrialLog in
Avatar of dignified
dignified

asked on

Regular Expression in Java (html tags)

I need a regular expression that works in Java that will take every <img src="whatever" .... > and insert this code inside the tag:

onmousedown="cancelBubbles();__cloneCallback(this.tagName, getElement()); return false;"

So the new string will be like:

OLD:  <img src="whatever" .... > 

NEW: <img onmousedown="cancelBubbles();__cloneCallback(this.tagName, getElement()); return false;" src="whatever" .... > 
Avatar of freeexpert
freeexpert

String.replaceAll(Pattern.quote("<img src="), "<img onmousedown=\"cancelBubbles();__cloneCallback(this.tagName, getElement()); return false;\" src="");

But you do understand all the cases where this will not quite do what you want, right?
Avatar of dignified

ASKER

Maybe, can you explain further?
Is your page coded in JSP?  if so then this will help you:

String oldstring = "<img src="whatever" .... >"
String newstring =  "<img onmousedown="cancelBubbles();__cloneCallback(this.tagName, getElement()); return false;" src="whatever" .... >"

oldstring = oldstring.replaceAll(oldstring,newstring)

other info:

http://java.sun.com/developer/technicalArticles/releases/1.4regex/
http://www.sitepoint.com/article/java-regex-api-explained

ellandrd
Avatar of CEHJ
tag = tag.replaceAll("(<img [^>]*?)>", "$1 onmousedown=\"cancelBubbles();__cloneCallback(this.tagName, getElement()); return false;\"");
> Maybe, can you explain further?

Are you going to be able to ensure that the replacement is only applied to tag? There are some cases when you might end up applying this unintentionally, e.g. when the pattern appears:

- in comments
- in CDATA
- Any string which is going to be xml encoded before it appears in the html.
Sorry - typo on my last. Should be

tag = tag.replaceAll("(<img [^>]*?)>", "$1 onmousedown=\"cancelBubbles();__cloneCallback(this.tagName, getElement()); return false;\">");
> CEHJ:
>      Sorry - typo on my last. Should be

Is there something wrong with the solution I gave, except that there is probably an extra double quote?
>>Is there something wrong with the solution I gave, except that there is probably an extra double quote?

I mustn't be quite awake this morning - what i gave doesn't address the problem. There's no obligation for the src attribute to appear first.

A better way would be to use an html parsing library. Try the Neko parser

what is wrong with my suggestion at inserting the new attributes before the src attribute?
> I mustn't be quite awake this morning - what i gave doesn't address the problem. There's no obligation for the src attribute to appear first.


Oh of course. I was only providing a very limited solution for replace <img src"> with the OP's replacement.
The onmousedown can appear anywhere. I guess I could just use a simple string replace and replace <img with all that I posted.

The only other trick I can think of is if there is already an onmousdown="" inside the tag, then I'd have to append my code to the end of the original, separated by semi colons, but it must be inserted before any return value.

I won't get that much into it for now.
ASKER CERTIFIED SOLUTION
Avatar of freeexpert
freeexpert

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial