0

I got a HTML string and from this I want to convert some special a tags to something else. I need this for a TinyMCE plugin. I tried to change Wordpress wpgallery plugin.

For example: These are in HTML string

<a href="http://www.yahoo.com">Yahoo</a> 
<a href="http://www.google.com">Google</a>
<a href="#" rel='special' title='link cat_id="4" content_id="5" content_slug="Slug 1"'>Some where else</a>

Here I have to find special link one and convert it to something else from it's title value like:

{link cat_id="4" content_id="5" content_slug="Slug 1"}

i need return value like this to insert it into MySQL

<a href="http://www.yahoo.com">Yahoo</a> 
<a href="http://www.google.com">Google</a>
{link cat_id="4" content_id="5" content_slug="Slug 1"}

I tried this

function getAttr(s, n) {
            n = new RegExp(n + '="([^"]+)"', 'g').exec(s);
            return n ? tinymce.DOM.decode(n[1]) : '';
        };

return co.replace(/[^<]*(<a href="([^"]+)">([^<]+)<\/a>)/g, function(a,im) {
var cls = getAttr(im, 'rel');
   if ( cls.indexOf('special') != -1 )
       return '{'+tinymce.trim(getAttr(im, 'title'))+'}';

   return a;
});

this

/[^<]*(<a href="([^"]+)">([^<]+)<\/a>)/g

does not find tags with rel eq to 'special' but all the others.

2
  • Try this: [^<]*(<a href="([^"]+)" ([\S]*=.*(\ )*)*>([^<]+)<\/a>) Commented Jul 8, 2011 at 11:20
  • i need this regexp for tinymce plugin.This works for onBeforeSetContent but not with onPostProcess. Do you have any idea? Commented Jul 8, 2011 at 16:30

1 Answer 1

2

You might want to look into the DOMDocument and related classes. They are much better at parsing HTML than a homebrewed regex solution would be.

You can create a DOMdocument using your supplied markup, execute getElementsByTagName to get all the hyperlinks, scan their attributes for a rel attribute with the value of special, and then take the appropriate action.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.