开发者

Javascript regex problem

开发者 https://www.devze.com 2023-01-08 20:15 出处:网络
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.

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";

开发者_JAVA百科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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消