Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #15062

Re: Remove portion of string unless it contains variable

From Mike Winter <usenet@michael-winter.me.invalid>
Newsgroups comp.lang.javascript
Subject Re: Remove portion of string unless it contains variable
References <9d0d2468-59b7-4b81-ab61-32875b019bb5@googlegroups.com>
Message-ID <NVIMr.802399$sD5.78708@fx03.am4> (permalink)
Organization virginmedia.com
Date 2012-07-16 01:06 +0100

Show all headers | View raw


On 14/07/2012 00:27, Jason C wrote:

> [snip]
>
> b = b.replace(/<img.*?src=["']*(.*?)["'}*.*?>/gi, '');
>
> How can I modify this to only replace if $1 does not contain "ignore"?

Something along the lines of:

   function removeImgElement(string, ignore) {
     return string.replace(/<img.+?src=(["'])([^\1>]*)\1[^>]*>/gi,
       function(substring, qm, attributeValue) {
         return (attributeValue.indexOf(ignore) == -1) ? '' : substring;
       });
   }

> As an addendum, what if I change ignore to:
>
> var ignore = new Array("example.com", "example_2.com");
>
> and want to exclude img tag that includes any of the array elements in $1?

Similarly:

   function removeImgElement(string, ignore) {
     if ((typeof ignore != 'object') || (ignore.constructor != Array))
       ignore = [String(ignore)];

     return string.replace(/<img.+?src=(["'])([^\1>]*)\1[^>]*>/gi,
       function(substring, qm, attributeValue) {
         for (var i = 0; i < ignore.length; i++)
           if (attributeValue.indexOf(ignore[i]) != -1)
             return substring;
         return '';
       });
   }

Hope that helps,
Mike


-- 
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Remove portion of string unless it contains variable Jason C <jwcarlton@gmail.com> - 2012-07-13 16:27 -0700
  Re: Remove portion of string unless it contains variable Jason C <jwcarlton@gmail.com> - 2012-07-13 17:51 -0700
    Re: Remove portion of string unless it contains variable "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-07-14 10:03 +0000
  Re: Remove portion of string unless it contains variable Mike Winter <usenet@michael-winter.me.invalid> - 2012-07-16 01:06 +0100

csiph-web