Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #15045 > unrolled thread
| Started by | Jason C <jwcarlton@gmail.com> |
|---|---|
| First post | 2012-07-13 16:27 -0700 |
| Last post | 2012-07-16 01:06 +0100 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.javascript
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
| From | Jason C <jwcarlton@gmail.com> |
|---|---|
| Date | 2012-07-13 16:27 -0700 |
| Subject | Remove portion of string unless it contains variable |
| Message-ID | <9d0d2468-59b7-4b81-ab61-32875b019bb5@googlegroups.com> |
Let's say that I have:
var ignore= "example.com";
var b = "Check out this awesome image!<br><br><img src='http://www.example.com/whatever.jpg' width='300' height='300' border='0'><br><br>Awesome.";
This comes from a message board that allows copy and paste from other sites, so the img tag can change a lot; the only thing for sure is that it will start with <img and have src=... in it.
I'm removing it using:
b = b.replace(/<img.*?src=["']*(.*?)["'}*.*?>/gi, '');
How can I modify this to only replace if $1 does not contain "ignore"?
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?
[toc] | [next] | [standalone]
| From | Jason C <jwcarlton@gmail.com> |
|---|---|
| Date | 2012-07-13 17:51 -0700 |
| Message-ID | <d3dee133-3533-426d-a442-0b59d93262af@googlegroups.com> |
| In reply to | #15045 |
On Friday, July 13, 2012 7:27:09 PM UTC-4, Jason C wrote: > b = b.replace(/<img.*?src=["']*(.*?)["'}*.*?>/gi, ''); Type; that should have been a square after "', not a curly: b = b.replace(/<img.*?src=["']*(.*?)["']*.*?>/gi, '');
[toc] | [prev] | [next] | [standalone]
| From | "Evertjan." <exxjxw.hannivoort@inter.nl.net> |
|---|---|
| Date | 2012-07-14 10:03 +0000 |
| Message-ID | <XnsA0907AB5012C4eejj99@194.109.133.133> |
| In reply to | #15047 |
Jason C wrote on 14 jul 2012 in comp.lang.javascript: > On Friday, July 13, 2012 7:27:09 PM UTC-4, Jason C wrote: >> b = b.replace(/<img.*?src=["']*(.*?)["'}*.*?>/gi, ''); > > Type; that should have been a square after "', not a curly: > > b = b.replace(/<img.*?src=["']*(.*?)["']*.*?>/gi, ''); Try this: b = b.replace(/<img.*?src=(["'])[^\1]+\1[^>]*>/gi, ''); \1 is replaced by the actual match of (["']) [^\1]+ quoted url should be not empty [^>]*> matches anything till and including the next > -- Evertjan. The Netherlands. (Please change the x'es to dots in my emailaddress)
[toc] | [prev] | [next] | [standalone]
| From | Mike Winter <usenet@michael-winter.me.invalid> |
|---|---|
| Date | 2012-07-16 01:06 +0100 |
| Message-ID | <NVIMr.802399$sD5.78708@fx03.am4> |
| In reply to | #15045 |
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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web