views:

37

answers:

1

I need to make html pages where I want to share Ad space with other adsense users. I am trying to use javascript to find and replace adsense code using regex expression but I am stuck.

This is the piece of string I am trying to match.

pub-111111111111111";

/* 336x280, created 12/12/12 */

google_ad_slot = "2222222222";

And this is the javascript containing regex expression that I tried (out of many other combination).

    <script type="text/javascript">
var html = document.body.innerHTML;
html = html.replace(/pub-([\d])*(";)\r(.)*\r(.)*([\d])(";)/i,'pub-444444444444444";google_ad_slot = "3333333333333";');
document.body.innerHTML = html;
</script>

But no luck. Anything RegEx other than pub-([\d])* part is not getting evaluated as I see using firebug.

A: 

Try changing

/pub-([\d])*(";)\r(.)*\r(.)*([\d])(";)/i

to

/pub-(\d*)(";)\n(.*)\n(.*)(\d)(";)/gim

You need to keep the * token next to the thing it modifies, not outside of a capture group. Also, the \n is more likely to match your newlines than \r. Or you can use [\r\n]+

Not sure the above regex will capture exactly what you're looking to capture, but my comments should get you closer to the truth.

Robusto