user1789437
user1789437

Reputation: 468

Regular Expression in dreamweaver

Can anyone help me turn this into a regular expresion?

<a onclick="NavigateChat();" style="cursor:pointer;"><img src="images/online-chat.jpg" width="350" height="150" border="0" alt="Title Loans Novato - Online Chat"/></a>

The alt tag will change, and so might the image, but <a onclick="NavigateChat();" style="cursor:pointer;"> will always start the string, and </a> will always end it.. How can I used a regex to find this?

Upvotes: 1

Views: 200

Answers (2)

Ro Yo Mi
Ro Yo Mi

Reputation: 15010

Description

I'm not quite sure what you're looking to return, so this generic regular expression will:

  • find anchor tags
  • require the anchor tag to have an attribute onclick="navigatechat();"
  • require the anchor tag to have an attribute style="cursor:pointer;"
  • allow the attributes to be matched in any order
  • require the anchor tag's inner text to be only an image tag
  • capture the anchor tag's inner text tag in it's entirety
  • avoid many of the edge cases which makes pattern matching in html difficult

<a(?=\s|>)(?=(?:[^>=|&)]*|='[^']*'|="[^"]*"|=[^'"][^\s>]*)*?\sonclick="NavigateChat\(\);")(?=(?:[^>=|&)]*|='[^']*'|="[^"]*"|=[^'"][^\s>]*)*?\sstyle="cursor:pointer;")(?:[^>=|&)]|='(?:[^']|\\')*'|="(?:[^"]|\\")*"|=[^'"][^\s>]*)*>\s*(<img\s.*?)\s*<\/a>

enter image description here

Example

Live Demo

Sample Text

<a onmouseover=' a=1; onclick="NavigateChat();" style="cursor:pointer;"  href="www.NotYourURL.com" ; if (3 <a && href="www.NotYourURL.com" && id="revSAR" && 6 > 3) { funRotate(href) ; } ; '  href='http://InterestedURL.com' id='revSAR'><img src="YouShouldn'tFindMe.nope"></a>

<a onclick="NavigateChat();" style="cursor:pointer;"><img src="images/online-chat.jpg" width="350" height="150" border="0" alt="Title Loans Novato - Online Chat"/></a>

Matches

Group 0 gets the entire matched anchor tag
Group 1 gets the inner text

[0][0] = <a onclick="NavigateChat();" style="cursor:pointer;"><img src="images/online-chat.jpg" width="350" height="150" border="0" alt="Title Loans Novato - Online Chat"/></a>
[0][1] = <img src="images/online-chat.jpg" width="350" height="150" border="0" alt="Title Loans Novato - Online Chat"/>

Upvotes: 1

beroe
beroe

Reputation: 12316

Do you need to extract/capture certain pieces of info or just find the whole string? My usual method for generalizing regexp is to start with the literal text and just replace elements with general placeholders...

<a onclick="NavigateChat\(\);" style="cursor:pointer;"><img src="[^"]+" width="\d+" height="\d+" border="\d+"  alt="[^"]+"/></a>

This expression uses the character set [^"] which stands for "not a quote mark". If you just use .* as a wildcard, your regexp will fail if there is more than one tag present in your document. Regexps are "greedy" and would try to select ALL the text from the first tag through to the end of the last link.

Without a data sample, I can't test this for sure, but it should be close.

Upvotes: 0

Related Questions