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 ();__clone Callback(t his.tagNam e, getElement()); return false;"
So the new string will be like:
OLD: <img src="whatever" .... >
NEW: <img onmousedown="cancelBubbles ();__clone Callback(t his.tagNam e, getElement()); return false;" src="whatever" .... >
onmousedown="cancelBubbles
So the new string will be like:
OLD: <img src="whatever" .... >
NEW: <img onmousedown="cancelBubbles
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 ();__clone Callback(t his.tagNam e, getElement()); return false;" src="whatever" .... >"
oldstring = oldstring.replaceAll(oldst ring,newst ring)
other info:
http://java.sun.com/developer/technicalArticles/releases/1.4regex/
http://www.sitepoint.com/article/java-regex-api-explained
ellandrd
String oldstring = "<img src="whatever" .... >"
String newstring = "<img onmousedown="cancelBubbles
oldstring = oldstring.replaceAll(oldst
other info:
http://java.sun.com/developer/technicalArticles/releases/1.4regex/
http://www.sitepoint.com/article/java-regex-api-explained
ellandrd
tag = tag.replaceAll("(<img [^>]*?)>", "$1 onmousedown=\"cancelBubble s();__clon eCallback( this.tagNa me, 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.
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=\"cancelBubble s();__clon eCallback( this.tagNa me, getElement()); return false;\">");
tag = tag.replaceAll("(<img [^>]*?)>", "$1 onmousedown=\"cancelBubble
> 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?
> 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
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.
Oh of course. I was only providing a very limited solution for replace <img src"> with the OP's replacement.
ASKER
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
But you do understand all the cases where this will not quite do what you want, right?