使用Javascript删除字符串的内容

Remove Content of String With Javascript

本文关键字:字符串 Javascript 删除 使用      更新时间:2023-09-26

我从API中提取新闻/体育/科技文章,并将这些文章填充到我的网页中。这里的问题是,在我的许多文章中,我总是得到一个包含javascript的文本字符串。

示例,我们一些文章的片段:

 Viewers can also choose to ignore the teaser that appears, and it will go away allowing you to watch the video uninterrupted. /* */ if (ads.isMobile()) { (function(window,$){ window.TechCrunch.loader.on('tc_ads_wrapper_omniture', function(){ if ( !window.ads.isAdPageSet ){ if ( typeof window.adSetAdURL == 'function' ) { window.adSetAdURL('/wp-content/themes/vip/techcrunch-2013/_uac/adpage.html'); window.ads.isAdPageSet = true; } } window.htmlAdWH('348-14-15-135e', "300", "115", 'f', 'adsDivafa83e683e'); }); }(this,this.jQuery)); } /* */ YouTube notes that over time, the plan is to not only help creators provide this enriched experience to viewers,

另一篇文章:

Their best strategy, I believe, is to communicate emotions rather than specific nouns and verbs. /* */ if (ads.isMobile()) { (function(window,$){ window.TechCrunch.loader.on('tc_ads_wrapper_omniture', function(){ if ( !window.ads.isAdPageSet ){ if ( typeof window.adSetAdURL == 'function' ) { window.adSetAdURL('/wp-content/themes/vip/techcrunch-2013/_uac/adpage.html'); window.ads.isAdPageSet = true; } } window.htmlAdWH('348-14-15-135e', "300", "115", 'f', 'adsDiv186350d01b'); }); }(this,this.jQuery)); } /* */ In a classic perceptual psychology experiment, pairs of strangers were asked to communicate emotions through touch alone. The “encoder” was given one of 12 different emotions, then touched the “decoder’s” forearm through a curtain.

和另一个:

“We’re known for digital content advertising, but close to 40% of our business has been mobile commerce for quite some time, in areas like user acquisition and monetization outside of the basic sales transaction.” /* */ if (ads.isMobile()) { (function(window,$){ window.TechCrunch.loader.on('tc_ads_wrapper_omniture', function(){ if ( !window.ads.isAdPageSet ){ if ( typeof window.adSetAdURL == 'function' ) { window.adSetAdURL('/wp-content/themes/vip/techcrunch-2013/_uac/adpage.html'); window.ads.isAdPageSet = true; } } window.htmlAdWH('348-14-15-135e', "300", "115", 'f', 'adsDivafa83e683e'); }); }(this,this.jQuery)); } /* */ The strategic nature of the investment from Market Tech was part of the reason why Lin said Glispa gave a nod to giving up a stake of the company.

在页面填充数据之前,有没有任何方法可以排除那些以/* */开头和结尾的javascript块。我查看了API调用,但实际上无法通过回调将其删除。

您可以使用regex来替换内容:

var regpattern = /('/'*'s'*'/(.*?)'/'*'s'*'/)/gm
var yourcontent = "include your content here";
yourcontent.replace(regpattern,"");