Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

We are MicroSoft. You will be assimilated. Resistance is futile. (Attributed to B.G., Gill Bates)


devel / comp.lang.javascript / Re: Help with regexp

SubjectAuthor
* Help with regexpAndrew Poulos
+* Re: Help with regexpAndrew Poulos
|`* Re: Help with regexpAndrew Poulos
| +* Re: Help with regexpJulio Di Egidio
| |+- Re: Help with regexpJulio Di Egidio
| |`* Re: Help with regexpAndrew Poulos
| | `- Re: Help with regexpJulio Di Egidio
| `* Re: Help with regexpJon Ribbens
|  `* Re: Help with regexpAndrew Poulos
|   `- Re: Help with regexpJon Ribbens
+- Re: Help with regexpThe Natural Philosopher
`* Re: Help with regexpThomas 'PointedEars' Lahn
 +- Re: Help with regexpThomas 'PointedEars' Lahn
 `* Re: Help with regexpAndrew Poulos
  `* Re: Help with regexpThomas 'PointedEars' Lahn
   `* Re: Help with regexpJon Ribbens
    `- Re: Help with regexpThomas 'PointedEars' Lahn

1
Help with regexp

<lZadnd6QivChfpH8nZ2dnUU7-L3NnZ2d@westnet.com.au>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17204&group=comp.lang.javascript#17204

  copy link   Newsgroups: comp.lang.javascript
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.snarked.org!border2.nntp.dca1.giganews.com!nntp.giganews.com!buffer2.nntp.dca1.giganews.com!buffer1.nntp.dca1.giganews.com!nntp.westnet.com.au!news.westnet.com.au.POSTED!not-for-mail
NNTP-Posting-Date: Fri, 06 Aug 2021 02:22:36 -0500
Newsgroups: comp.lang.javascript
X-Mozilla-News-Host: news://news.iinet.net.au:119
From: ap_prog@hotmail.com (Andrew Poulos)
Subject: Help with regexp
Date: Fri, 6 Aug 2021 17:22:33 +1000
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.12.0
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-AU
Content-Transfer-Encoding: 7bit
Message-ID: <lZadnd6QivChfpH8nZ2dnUU7-L3NnZ2d@westnet.com.au>
Lines: 22
X-Usenet-Provider: http://www.giganews.com
NNTP-Posting-Host: 110.175.24.76
X-Trace: sv3-rsZ0pH7b2D1Oji+9Ckh4BzxkwD1iWO2Bn9oZHNt2XM0hTEZZagv3bv4HEF1z3LWCLFfDLddATfxENJb!CvCJ2O67tks3Au/2pmS9YIrMqZTl3UUcb8URN8+mAiQAfAlOmDTWZjssG8L9E6Q1X8yUeTwMr/wO!uWNWucoo3l2+j/Zst0Mx
X-Complaints-To: abuse@westnet.com.au
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 1722
 by: Andrew Poulos - Fri, 6 Aug 2021 07:22 UTC

I have a string that looks like this

let str = 'linear-gradient(90deg, #ff0000 50%, #00ff00 100%)"

I need to convert the hex values to rgba so I've written a function

const convertHex2Rgba = (hex, alpha = 1) => {
...
return `rgba(${r},${g},${b},${alpha})`;
};

I'm stuck trying to write a regexp that replaces the hex values (that
may have either 3 or 6 characters - plus the #) with the value returned
from convertHex2Rgba.

This (one of my many failed attempts):

str.replace(/#[0-9a-fA-F]/, convertHex2Rgba('$1'));

does nothing.

Andrew Poulos

Re: Help with regexp

<e8KdndKUXq4_epH8nZ2dnUU7-bOdnZ2d@westnet.com.au>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17205&group=comp.lang.javascript#17205

  copy link   Newsgroups: comp.lang.javascript
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.snarked.org!border2.nntp.dca1.giganews.com!nntp.giganews.com!buffer2.nntp.dca1.giganews.com!nntp.westnet.com.au!news.westnet.com.au.POSTED!not-for-mail
NNTP-Posting-Date: Fri, 06 Aug 2021 02:41:22 -0500
Subject: Re: Help with regexp
Newsgroups: comp.lang.javascript
References: <lZadnd6QivChfpH8nZ2dnUU7-L3NnZ2d@westnet.com.au>
From: ap_prog@hotmail.com (Andrew Poulos)
Date: Fri, 6 Aug 2021 17:41:22 +1000
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.12.0
MIME-Version: 1.0
In-Reply-To: <lZadnd6QivChfpH8nZ2dnUU7-L3NnZ2d@westnet.com.au>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-AU
Content-Transfer-Encoding: 8bit
Message-ID: <e8KdndKUXq4_epH8nZ2dnUU7-bOdnZ2d@westnet.com.au>
Lines: 29
X-Usenet-Provider: http://www.giganews.com
NNTP-Posting-Host: 110.175.24.76
X-Trace: sv3-kW4XsuYGLXYOPndiGbxJV1Sv6vp6izFmZlAkzpxSQNkQLQnu15RLZlrHMUVehaho5aBHCm5FbzYjWC8!UNz3du/jMLZMX1eETBUl/dCI0Shm4tub9OSLJcttlgxvH112sUpvAl4C9teMfvR8+/zbcO0QrDFw!hFWnVcrYqi5BVXTbSQep
X-Complaints-To: abuse@westnet.com.au
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 2024
 by: Andrew Poulos - Fri, 6 Aug 2021 07:41 UTC

On 6/08/2021 5:22 pm, Andrew Poulos wrote:
> I have a string that looks like this
>
> let str = 'linear-gradient(90deg, #ff0000 50%, #00ff00 100%)"
>
> I need to convert the hex values to rgba so I've written a function
>
> const convertHex2Rgba = (hex, alpha = 1) => {
>   ...
>   return `rgba(${r},${g},${b},${alpha})`;
> };
>
> I'm stuck trying to write a regexp that replaces the hex values (that
> may have either 3 or 6 characters - plus the #) with the value returned
> from convertHex2Rgba.
>
> This (one of my many failed attempts):
>
> str.replace(/#[0-9a-fA-F]/, convertHex2Rgba('$1'));
>
> does nothing.

This has got me a bit closer

str.replace(/#([A-Fa-f0-9]{3}){1,2}/, convertHex2Rgba('$1'));

but the match doesn't get replaced.

Andrew Poulos

Re: Help with regexp

<Eu-dndUUEZebdpH8nZ2dnUU7-QudnZ2d@westnet.com.au>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17206&group=comp.lang.javascript#17206

  copy link   Newsgroups: comp.lang.javascript
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.snarked.org!border2.nntp.dca1.giganews.com!nntp.giganews.com!buffer2.nntp.dca1.giganews.com!nntp.westnet.com.au!news.westnet.com.au.POSTED!not-for-mail
NNTP-Posting-Date: Fri, 06 Aug 2021 02:55:50 -0500
Subject: Re: Help with regexp
Newsgroups: comp.lang.javascript
References: <lZadnd6QivChfpH8nZ2dnUU7-L3NnZ2d@westnet.com.au>
<e8KdndKUXq4_epH8nZ2dnUU7-bOdnZ2d@westnet.com.au>
From: ap_prog@hotmail.com (Andrew Poulos)
Date: Fri, 6 Aug 2021 17:55:50 +1000
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.12.0
MIME-Version: 1.0
In-Reply-To: <e8KdndKUXq4_epH8nZ2dnUU7-bOdnZ2d@westnet.com.au>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-AU
Content-Transfer-Encoding: 8bit
Message-ID: <Eu-dndUUEZebdpH8nZ2dnUU7-QudnZ2d@westnet.com.au>
Lines: 38
X-Usenet-Provider: http://www.giganews.com
NNTP-Posting-Host: 110.175.24.76
X-Trace: sv3-afAXyZkFd1Vg5bM4IewgKteH1z6XIXXgrzoNH+z2V8YHzsCQ2QRjeGK29fyHBgnseRlVM2ePecvUU7f!ylm5eSho+7Ez64hH5QwxT7L//Ylvlyo+Tua8VlbsiT+YR5zNV0tOANMym9NHn/iiyDOpqMmsmR19!Ol712ID5Y8q9oIVKGEBi
X-Complaints-To: abuse@westnet.com.au
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 2303
 by: Andrew Poulos - Fri, 6 Aug 2021 07:55 UTC

On 6/08/2021 5:41 pm, Andrew Poulos wrote:
> On 6/08/2021 5:22 pm, Andrew Poulos wrote:
>> I have a string that looks like this
>>
>> let str = 'linear-gradient(90deg, #ff0000 50%, #00ff00 100%)"
>>
>> I need to convert the hex values to rgba so I've written a function
>>
>> const convertHex2Rgba = (hex, alpha = 1) => {
>>    ...
>>    return `rgba(${r},${g},${b},${alpha})`;
>> };
>>
>> I'm stuck trying to write a regexp that replaces the hex values (that
>> may have either 3 or 6 characters - plus the #) with the value
>> returned from convertHex2Rgba.
>>
>> This (one of my many failed attempts):
>>
>> str.replace(/#[0-9a-fA-F]/, convertHex2Rgba('$1'));
>>
>> does nothing.
>
> This has got me a bit closer
>
> str.replace(/#([A-Fa-f0-9]{3}){1,2}/, convertHex2Rgba('$1'));
>
> but the match doesn't get replaced.

str.replace(/#([A-Fa-f0-9]{3}){1,2}/, match => {
return convertHex2Rgba(match);
});

Nearly there, but I have to do it one match at a time.

Andrew Poulos

Re: Help with regexp

<89c9d990-bac6-4690-8c82-b5cf74aa43bbn@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17207&group=comp.lang.javascript#17207

  copy link   Newsgroups: comp.lang.javascript
X-Received: by 2002:ac8:6b19:: with SMTP id w25mr4087520qts.222.1628239145563;
Fri, 06 Aug 2021 01:39:05 -0700 (PDT)
X-Received: by 2002:a05:6808:f8b:: with SMTP id o11mr6357249oiw.48.1628239145288;
Fri, 06 Aug 2021 01:39:05 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.javascript
Date: Fri, 6 Aug 2021 01:39:05 -0700 (PDT)
In-Reply-To: <Eu-dndUUEZebdpH8nZ2dnUU7-QudnZ2d@westnet.com.au>
Injection-Info: google-groups.googlegroups.com; posting-host=93.41.96.50; posting-account=F3H0JAgAAADcYVukktnHx7hFG5stjWse
NNTP-Posting-Host: 93.41.96.50
References: <lZadnd6QivChfpH8nZ2dnUU7-L3NnZ2d@westnet.com.au>
<e8KdndKUXq4_epH8nZ2dnUU7-bOdnZ2d@westnet.com.au> <Eu-dndUUEZebdpH8nZ2dnUU7-QudnZ2d@westnet.com.au>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <89c9d990-bac6-4690-8c82-b5cf74aa43bbn@googlegroups.com>
Subject: Re: Help with regexp
From: julio@diegidio.name (Julio Di Egidio)
Injection-Date: Fri, 06 Aug 2021 08:39:05 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Julio Di Egidio - Fri, 6 Aug 2021 08:39 UTC

On Friday, 6 August 2021 at 09:56:02 UTC+2, Andrew Poulos wrote:
> On 6/08/2021 5:41 pm, Andrew Poulos wrote:
> > On 6/08/2021 5:22 pm, Andrew Poulos wrote:
> >> I have a string that looks like this
> >>
> >> let str = 'linear-gradient(90deg, #ff0000 50%, #00ff00 100%)"
> >>
> >> I need to convert the hex values to rgba so I've written a function
> >>
> >> const convertHex2Rgba = (hex, alpha = 1) => {
> >> ...
> >> return `rgba(${r},${g},${b},${alpha})`;
> >> };
> >>
> >> I'm stuck trying to write a regexp that replaces the hex values (that
> >> may have either 3 or 6 characters - plus the #) with the value
> >> returned from convertHex2Rgba.
> >>
> >> This (one of my many failed attempts):
> >>
> >> str.replace(/#[0-9a-fA-F]/, convertHex2Rgba('$1'));
> >>
> >> does nothing.
> >
> > This has got me a bit closer
> >
> > str.replace(/#([A-Fa-f0-9]{3}){1,2}/, convertHex2Rgba('$1'));
> >
> > but the match doesn't get replaced.
> str.replace(/#([A-Fa-f0-9]{3}){1,2}/, match => {
> return convertHex2Rgba(match);
> });

Your capturing group in "([A-Fa-f0-9]{3})" which only matches "#" followed by three hex digits: the "{1,2}" won't change that, it means repeated occurrences of that same pattern.

> Nearly there, but I have to do it one match at a time.

Because you are missing the 'g' flag, for "global" search and substitution. I have come up with this one, which is as simple as i could make it (while staying correct):

/(#[A-Fa-f0-9]{6}|#[A-Fa-f0-9]{3})/gs

Have a look at the docs and the examples:
<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#using_an_inline_function_that_modifies_the_matched_characters>

Also, you can prototype with online tools, e.g. as I have done here: <https://regex101.com/r/BdPOyA/1> (you cannot use a replacer function there, but at least you know your pattern is correct).

HTH,

Julio

Re: Help with regexp

<e86af691-08fd-44fe-b9c2-b6576f1b6437n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17208&group=comp.lang.javascript#17208

  copy link   Newsgroups: comp.lang.javascript
X-Received: by 2002:a05:622a:1910:: with SMTP id w16mr7888783qtc.227.1628240003861;
Fri, 06 Aug 2021 01:53:23 -0700 (PDT)
X-Received: by 2002:a05:6808:f02:: with SMTP id m2mr6713585oiw.0.1628240003486;
Fri, 06 Aug 2021 01:53:23 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.javascript
Date: Fri, 6 Aug 2021 01:53:23 -0700 (PDT)
In-Reply-To: <89c9d990-bac6-4690-8c82-b5cf74aa43bbn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=93.41.96.50; posting-account=F3H0JAgAAADcYVukktnHx7hFG5stjWse
NNTP-Posting-Host: 93.41.96.50
References: <lZadnd6QivChfpH8nZ2dnUU7-L3NnZ2d@westnet.com.au>
<e8KdndKUXq4_epH8nZ2dnUU7-bOdnZ2d@westnet.com.au> <Eu-dndUUEZebdpH8nZ2dnUU7-QudnZ2d@westnet.com.au>
<89c9d990-bac6-4690-8c82-b5cf74aa43bbn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <e86af691-08fd-44fe-b9c2-b6576f1b6437n@googlegroups.com>
Subject: Re: Help with regexp
From: julio@diegidio.name (Julio Di Egidio)
Injection-Date: Fri, 06 Aug 2021 08:53:23 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Julio Di Egidio - Fri, 6 Aug 2021 08:53 UTC

On Friday, 6 August 2021 at 10:39:10 UTC+2, Julio Di Egidio wrote:
> On Friday, 6 August 2021 at 09:56:02 UTC+2, Andrew Poulos wrote:
> > On 6/08/2021 5:41 pm, Andrew Poulos wrote:

> > > but the match doesn't get replaced.
> > str.replace(/#([A-Fa-f0-9]{3}){1,2}/, match => {
> > return convertHex2Rgba(match);
> > });
>
> Your capturing group in "([A-Fa-f0-9]{3})" which only matches "#" followed by three hex digits: the "{1,2}" won't change that, it means repeated occurrences of that same pattern.

No, I have misread that, for one thing "#" is out of the match... Never mind, that is why I rather prototype. ;)

Julio

Re: Help with regexp

<slrnsgq462.49p.jon+usenet@raven.unequivocal.eu>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17211&group=comp.lang.javascript#17211

  copy link   Newsgroups: comp.lang.javascript
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jon+usenet@unequivocal.eu (Jon Ribbens)
Newsgroups: comp.lang.javascript
Subject: Re: Help with regexp
Date: Fri, 6 Aug 2021 10:36:50 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 46
Message-ID: <slrnsgq462.49p.jon+usenet@raven.unequivocal.eu>
References: <lZadnd6QivChfpH8nZ2dnUU7-L3NnZ2d@westnet.com.au>
<e8KdndKUXq4_epH8nZ2dnUU7-bOdnZ2d@westnet.com.au>
<Eu-dndUUEZebdpH8nZ2dnUU7-QudnZ2d@westnet.com.au>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 6 Aug 2021 10:36:50 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="05b4852fb3707fc8ce384c047bfef277";
logging-data="21072"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18EXKDLKWF2ZoaNjcpdSFpC6/gmEfaard4="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:psXg0B/4sYJXn7Vems2e8xc7Mck=
 by: Jon Ribbens - Fri, 6 Aug 2021 10:36 UTC

On 2021-08-06, Andrew Poulos <ap_prog@hotmail.com> wrote:
> On 6/08/2021 5:41 pm, Andrew Poulos wrote:
>> On 6/08/2021 5:22 pm, Andrew Poulos wrote:
>>> I have a string that looks like this
>>>
>>> let str = 'linear-gradient(90deg, #ff0000 50%, #00ff00 100%)"
>>>
>>> I need to convert the hex values to rgba so I've written a function
>>>
>>> const convertHex2Rgba = (hex, alpha = 1) => {
>>>    ...
>>>    return `rgba(${r},${g},${b},${alpha})`;
>>> };
>>>
>>> I'm stuck trying to write a regexp that replaces the hex values (that
>>> may have either 3 or 6 characters - plus the #) with the value
>>> returned from convertHex2Rgba.
>>>
>>> This (one of my many failed attempts):
>>>
>>> str.replace(/#[0-9a-fA-F]/, convertHex2Rgba('$1'));
>>>
>>> does nothing.
>>
>> This has got me a bit closer
>>
>> str.replace(/#([A-Fa-f0-9]{3}){1,2}/, convertHex2Rgba('$1'));
>>
>> but the match doesn't get replaced.
>
> str.replace(/#([A-Fa-f0-9]{3}){1,2}/, match => {
> return convertHex2Rgba(match);
> });
>
> Nearly there, but I have to do it one match at a time.

You need the /g flag to tell it to do more than one match.
You could also use the /i flag to be case-insensitive:

str.replace(/#([0-9a-f]{3}){1,2}/gi, match => convertHex2Rgba(match))

You might also want to make sure that you're not seeing *more* than
6 hexadecimal characters:

/#([0-9a-f]{3}){1,2}(?![0-9a-f])/gi

Re: Help with regexp

<sejtu5$59s$9@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17212&group=comp.lang.javascript#17212

  copy link   Newsgroups: comp.lang.javascript
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: tnp@invalid.invalid (The Natural Philosopher)
Newsgroups: comp.lang.javascript
Subject: Re: Help with regexp
Date: Fri, 6 Aug 2021 19:10:13 +0100
Organization: A little, after lunch
Lines: 33
Message-ID: <sejtu5$59s$9@dont-email.me>
References: <lZadnd6QivChfpH8nZ2dnUU7-L3NnZ2d@westnet.com.au>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 6 Aug 2021 18:10:13 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="d0849c180e6eec60df912a3381754def";
logging-data="5436"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+nVtxnRpF//MvF4sqIrw1/UQw3Cx65B5o="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101
Thunderbird/60.6.1
Cancel-Lock: sha1:F5zUgUZstT8mXA60en15Bo8uk7U=
In-Reply-To: <lZadnd6QivChfpH8nZ2dnUU7-L3NnZ2d@westnet.com.au>
Content-Language: en-GB
 by: The Natural Philosop - Fri, 6 Aug 2021 18:10 UTC

On 06/08/2021 08:22, Andrew Poulos wrote:
> I have a string that looks like this
>
> let str = 'linear-gradient(90deg, #ff0000 50%, #00ff00 100%)"
>
> I need to convert the hex values to rgba so I've written a function
>
> const convertHex2Rgba = (hex, alpha = 1) => {
>   ...
>   return `rgba(${r},${g},${b},${alpha})`;
> };
>
> I'm stuck trying to write a regexp that replaces the hex values (that
> may have either 3 or 6 characters - plus the #) with the value returned
> from convertHex2Rgba.
>
My personal experience is that you will have written code to do that
long before you discover the magic regexp.

Regexp INCREASES programming time!

> This (one of my many failed attempts):
>
> str.replace(/#[0-9a-fA-F]/, convertHex2Rgba('$1'));
>
> does nothing.
>
> Andrew Poulos

--
The New Left are the people they warned you about.

Re: Help with regexp

<c4SdnXWN5JyAXpD8nZ2dnUU7-XvNnZ2d@westnet.com.au>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17213&group=comp.lang.javascript#17213

  copy link   Newsgroups: comp.lang.javascript
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!tr1.eu1.usenetexpress.com!feeder.usenetexpress.com!tr2.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!buffer1.nntp.dca1.giganews.com!nntp.westnet.com.au!news.westnet.com.au.POSTED!not-for-mail
NNTP-Posting-Date: Fri, 06 Aug 2021 18:17:49 -0500
Subject: Re: Help with regexp
Newsgroups: comp.lang.javascript
References: <lZadnd6QivChfpH8nZ2dnUU7-L3NnZ2d@westnet.com.au> <e8KdndKUXq4_epH8nZ2dnUU7-bOdnZ2d@westnet.com.au> <Eu-dndUUEZebdpH8nZ2dnUU7-QudnZ2d@westnet.com.au> <slrnsgq462.49p.jon+usenet@raven.unequivocal.eu>
From: ap_prog@hotmail.com (Andrew Poulos)
Date: Sat, 7 Aug 2021 09:17:47 +1000
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.12.0
MIME-Version: 1.0
In-Reply-To: <slrnsgq462.49p.jon+usenet@raven.unequivocal.eu>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-AU
Content-Transfer-Encoding: 8bit
Message-ID: <c4SdnXWN5JyAXpD8nZ2dnUU7-XvNnZ2d@westnet.com.au>
Lines: 54
X-Usenet-Provider: http://www.giganews.com
NNTP-Posting-Host: 110.175.24.76
X-Trace: sv3-bR9/BTG6IMWvdd4/LhZb5j2HENAi07I5EQ7qWov2tfzMfLxahc2wUp/Co4TPfzEAuqvq6vsbAGFDzIF!rKFpLNpTppvTktorn0a2P+ux9/oP9hKK7agiVxZ+JXoCdNixA3J4wwW4CAblWMzwYZb6QW2XxJQr!EcvtrQ2jeoR0VPa1hQdE
X-Complaints-To: abuse@westnet.com.au
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 3120
 by: Andrew Poulos - Fri, 6 Aug 2021 23:17 UTC

On 6/08/2021 8:36 pm, Jon Ribbens wrote:
> On 2021-08-06, Andrew Poulos <ap_prog@hotmail.com> wrote:
>> On 6/08/2021 5:41 pm, Andrew Poulos wrote:
>>> On 6/08/2021 5:22 pm, Andrew Poulos wrote:
>>>> I have a string that looks like this
>>>>
>>>> let str = 'linear-gradient(90deg, #ff0000 50%, #00ff00 100%)"
>>>>
>>>> I need to convert the hex values to rgba so I've written a function
>>>>
>>>> const convertHex2Rgba = (hex, alpha = 1) => {
>>>>    ...
>>>>    return `rgba(${r},${g},${b},${alpha})`;
>>>> };
>>>>
>>>> I'm stuck trying to write a regexp that replaces the hex values (that
>>>> may have either 3 or 6 characters - plus the #) with the value
>>>> returned from convertHex2Rgba.
>>>>
>>>> This (one of my many failed attempts):
>>>>
>>>> str.replace(/#[0-9a-fA-F]/, convertHex2Rgba('$1'));
>>>>
>>>> does nothing.
>>>
>>> This has got me a bit closer
>>>
>>> str.replace(/#([A-Fa-f0-9]{3}){1,2}/, convertHex2Rgba('$1'));
>>>
>>> but the match doesn't get replaced.
>>
>> str.replace(/#([A-Fa-f0-9]{3}){1,2}/, match => {
>> return convertHex2Rgba(match);
>> });
>>
>> Nearly there, but I have to do it one match at a time.
>
> You need the /g flag to tell it to do more than one match.
> You could also use the /i flag to be case-insensitive:
>
> str.replace(/#([0-9a-f]{3}){1,2}/gi, match => convertHex2Rgba(match))
>
> You might also want to make sure that you're not seeing *more* than
> 6 hexadecimal characters:
>
> /#([0-9a-f]{3}){1,2}(?![0-9a-f])/gi

Doesn't the {3}{1,2} mean, match one or two groups of 3 characters? So I
don't need to check for more than 6. If there's more than 6 then it's
either hexa or invalid.

Andrew Poulos

Andrew Poulos

Re: Help with regexp

<I56dnWnoV8QGXpD8nZ2dnUU7-dPNnZ2d@westnet.com.au>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17214&group=comp.lang.javascript#17214

  copy link   Newsgroups: comp.lang.javascript
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.snarked.org!border2.nntp.dca1.giganews.com!nntp.giganews.com!buffer2.nntp.dca1.giganews.com!nntp.westnet.com.au!news.westnet.com.au.POSTED!not-for-mail
NNTP-Posting-Date: Fri, 06 Aug 2021 18:19:55 -0500
Subject: Re: Help with regexp
Newsgroups: comp.lang.javascript
References: <lZadnd6QivChfpH8nZ2dnUU7-L3NnZ2d@westnet.com.au>
<e8KdndKUXq4_epH8nZ2dnUU7-bOdnZ2d@westnet.com.au>
<Eu-dndUUEZebdpH8nZ2dnUU7-QudnZ2d@westnet.com.au>
<89c9d990-bac6-4690-8c82-b5cf74aa43bbn@googlegroups.com>
From: ap_prog@hotmail.com (Andrew Poulos)
Date: Sat, 7 Aug 2021 09:19:53 +1000
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.12.0
MIME-Version: 1.0
In-Reply-To: <89c9d990-bac6-4690-8c82-b5cf74aa43bbn@googlegroups.com>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-AU
Content-Transfer-Encoding: 7bit
Message-ID: <I56dnWnoV8QGXpD8nZ2dnUU7-dPNnZ2d@westnet.com.au>
Lines: 55
X-Usenet-Provider: http://www.giganews.com
NNTP-Posting-Host: 110.175.24.76
X-Trace: sv3-Gula0IUv2vrH/5qPGADXxMLyccViw9LMxJE4/ErK5se3+qBnMTQG69/YV9jcx0pXtmfcRqODEQNXSGj!b8OlzlaOmc+R4eCfncImWx+ur78NC3YyLWvFUU/73mOmbWV0+pfr+5+fDe3OSMk55xVaat8hhnEu!UStR9MAffYWSCEX4ieL/
X-Complaints-To: abuse@westnet.com.au
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 3580
 by: Andrew Poulos - Fri, 6 Aug 2021 23:19 UTC

On 6/08/2021 6:39 pm, Julio Di Egidio wrote:
> On Friday, 6 August 2021 at 09:56:02 UTC+2, Andrew Poulos wrote:
>> On 6/08/2021 5:41 pm, Andrew Poulos wrote:
>>> On 6/08/2021 5:22 pm, Andrew Poulos wrote:
>>>> I have a string that looks like this
>>>>
>>>> let str = 'linear-gradient(90deg, #ff0000 50%, #00ff00 100%)"
>>>>
>>>> I need to convert the hex values to rgba so I've written a function
>>>>
>>>> const convertHex2Rgba = (hex, alpha = 1) => {
>>>> ...
>>>> return `rgba(${r},${g},${b},${alpha})`;
>>>> };
>>>>
>>>> I'm stuck trying to write a regexp that replaces the hex values (that
>>>> may have either 3 or 6 characters - plus the #) with the value
>>>> returned from convertHex2Rgba.
>>>>
>>>> This (one of my many failed attempts):
>>>>
>>>> str.replace(/#[0-9a-fA-F]/, convertHex2Rgba('$1'));
>>>>
>>>> does nothing.
>>>
>>> This has got me a bit closer
>>>
>>> str.replace(/#([A-Fa-f0-9]{3}){1,2}/, convertHex2Rgba('$1'));
>>>
>>> but the match doesn't get replaced.
>> str.replace(/#([A-Fa-f0-9]{3}){1,2}/, match => {
>> return convertHex2Rgba(match);
>> });
>
> Your capturing group in "([A-Fa-f0-9]{3})" which only matches "#" followed by three hex digits: the "{1,2}" won't change that, it means repeated occurrences of that same pattern.

Thanks. though my testing seems to sow that it picks up 6 characters.

Rgeexp (like SQL) seems to me to be like a language one has to be born
speaking ;-)

>> Nearly there, but I have to do it one match at a time.
>
> Because you are missing the 'g' flag, for "global" search and substitution. I have come up with this one, which is as simple as i could make it (while staying correct):
>
> /(#[A-Fa-f0-9]{6}|#[A-Fa-f0-9]{3})/gs
>
> Have a look at the docs and the examples:
> <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#using_an_inline_function_that_modifies_the_matched_characters>
>
> Also, you can prototype with online tools, e.g. as I have done here: <https://regex101.com/r/BdPOyA/1> (you cannot use a replacer function there, but at least you know your pattern is correct).

Nice link.

Andrew Poulos

Re: Help with regexp

<9ec18c8f-d220-4938-b044-99d8477d7511n@googlegroups.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17215&group=comp.lang.javascript#17215

  copy link   Newsgroups: comp.lang.javascript
X-Received: by 2002:a05:622a:1014:: with SMTP id d20mr11237576qte.360.1628295964758; Fri, 06 Aug 2021 17:26:04 -0700 (PDT)
X-Received: by 2002:a9d:37c9:: with SMTP id x67mr9087109otb.2.1628295963886; Fri, 06 Aug 2021 17:26:03 -0700 (PDT)
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!tr2.eu1.usenetexpress.com!feeder.usenetexpress.com!tr1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.javascript
Date: Fri, 6 Aug 2021 17:26:03 -0700 (PDT)
In-Reply-To: <I56dnWnoV8QGXpD8nZ2dnUU7-dPNnZ2d@westnet.com.au>
Injection-Info: google-groups.googlegroups.com; posting-host=93.41.96.50; posting-account=F3H0JAgAAADcYVukktnHx7hFG5stjWse
NNTP-Posting-Host: 93.41.96.50
References: <lZadnd6QivChfpH8nZ2dnUU7-L3NnZ2d@westnet.com.au> <e8KdndKUXq4_epH8nZ2dnUU7-bOdnZ2d@westnet.com.au> <Eu-dndUUEZebdpH8nZ2dnUU7-QudnZ2d@westnet.com.au> <89c9d990-bac6-4690-8c82-b5cf74aa43bbn@googlegroups.com> <I56dnWnoV8QGXpD8nZ2dnUU7-dPNnZ2d@westnet.com.au>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <9ec18c8f-d220-4938-b044-99d8477d7511n@googlegroups.com>
Subject: Re: Help with regexp
From: julio@diegidio.name (Julio Di Egidio)
Injection-Date: Sat, 07 Aug 2021 00:26:04 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 14
 by: Julio Di Egidio - Sat, 7 Aug 2021 00:26 UTC

On Saturday, 7 August 2021 at 01:20:07 UTC+2, Andrew Poulos wrote:
> On 6/08/2021 6:39 pm, Julio Di Egidio wrote:

> Rgeexp (like SQL) seems to me to be like a language one has to be born
> speaking ;-)

Don't just give a man a fish... Sure, these constructs can get quite intricated to keep track of, which is why "prototyping" is highly advised for pattern writing and checking, anyway, should you find yourself writing regex all day long, be reassured that you'd soon find yourself (mostly) writing it by heart: or, at least as effectively as you would any other language.

Have fun,

Julio

Re: Help with regexp

<slrnsgtlta.49p.jon+usenet@raven.unequivocal.eu>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17217&group=comp.lang.javascript#17217

  copy link   Newsgroups: comp.lang.javascript
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jon+usenet@unequivocal.eu (Jon Ribbens)
Newsgroups: comp.lang.javascript
Subject: Re: Help with regexp
Date: Sat, 7 Aug 2021 18:57:47 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <slrnsgtlta.49p.jon+usenet@raven.unequivocal.eu>
References: <lZadnd6QivChfpH8nZ2dnUU7-L3NnZ2d@westnet.com.au>
<e8KdndKUXq4_epH8nZ2dnUU7-bOdnZ2d@westnet.com.au>
<Eu-dndUUEZebdpH8nZ2dnUU7-QudnZ2d@westnet.com.au>
<slrnsgq462.49p.jon+usenet@raven.unequivocal.eu>
<c4SdnXWN5JyAXpD8nZ2dnUU7-XvNnZ2d@westnet.com.au>
Injection-Date: Sat, 7 Aug 2021 18:57:47 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="9f025d789ffd5b8be450cf090c48300f";
logging-data="28682"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+U5f3oH2PA34kmmsPX+SN59yCEjhzadzU="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:mKert+iZf4FSROFIA3PfJYcsCIo=
 by: Jon Ribbens - Sat, 7 Aug 2021 18:57 UTC

On 2021-08-06, Andrew Poulos <ap_prog@hotmail.com> wrote:
> On 6/08/2021 8:36 pm, Jon Ribbens wrote:
>>> str.replace(/#([A-Fa-f0-9]{3}){1,2}/, match => {
>>> return convertHex2Rgba(match);
>>> });
>>>
>>> Nearly there, but I have to do it one match at a time.
>>
>> You need the /g flag to tell it to do more than one match.
>> You could also use the /i flag to be case-insensitive:
>>
>> str.replace(/#([0-9a-f]{3}){1,2}/gi, match => convertHex2Rgba(match))
>>
>> You might also want to make sure that you're not seeing *more* than
>> 6 hexadecimal characters:
>>
>> /#([0-9a-f]{3}){1,2}(?![0-9a-f])/gi
>
> Doesn't the {3}{1,2} mean, match one or two groups of 3 characters? So I
> don't need to check for more than 6. If there's more than 6 then it's
> either hexa or invalid.

It depends what you want to do if the string contains something like
'#123456789'. You might say that you don't think that will ever happen,
but personally I prefer to err on the side of caution. Also, bear in
mind if you're just chucking random bits of CSS at this then other CSS
tokens such as strings, selectors, comments, and URLs can contain things
matching your regexp (whether or not you use my more-cautious version).

Re: Help with regexp

<3136550.aeNJFYEL58@PointedEars.de>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17218&group=comp.lang.javascript#17218

  copy link   Newsgroups: comp.lang.javascript
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.mb-net.net!open-news-network.org!.POSTED.178.197.212.81!not-for-mail
From: PointedEars@web.de (Thomas 'PointedEars' Lahn)
Newsgroups: comp.lang.javascript
Subject: Re: Help with regexp
Date: Sat, 07 Aug 2021 21:56:28 +0200
Organization: PointedEars Software (PES)
Lines: 54
Message-ID: <3136550.aeNJFYEL58@PointedEars.de>
References: <lZadnd6QivChfpH8nZ2dnUU7-L3NnZ2d@westnet.com.au>
Reply-To: Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 8Bit
Injection-Info: gwaiyur.mb-net.net; posting-host="178.197.212.81";
logging-data="14850"; mail-complaints-to="abuse@open-news-network.org"
User-Agent: KNode/4.14.10
Cancel-Lock: sha1:MGEInl1a6+/TqD4IeWCOEZQA7eM=
X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
X-User-ID: U2FsdGVkX1+4V5/REat6kDkGtV72ruuQoOjUuxgANe52GpP9qyzKAw==
Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
 by: Thomas 'Pointed - Sat, 7 Aug 2021 19:56 UTC

Andrew Poulos wrote:

> I have a string that looks like this
>
> let str = 'linear-gradient(90deg, #ff0000 50%, #00ff00 100%)"
>
> I need to convert the hex values to rgba so I've written a function
>
> const convertHex2Rgba = (hex, alpha = 1) => {
> ...
> return `rgba(${r},${g},${b},${alpha})`;
> };
>
> I'm stuck trying to write a regexp that replaces the hex values (that
> may have either 3 or 6 characters - plus the #) with the value returned
> from convertHex2Rgba.
>
> This (one of my many failed attempts):
>
> str.replace(/#[0-9a-fA-F]/, convertHex2Rgba('$1'));
>
> does nothing.

Of course not.

1. You are not using the return value of the “replace” method.

2. You are not passing your function a proper value for the formal
parameter “hex”, but the string value '$1' instead.
('$1' is only expanded with the first captured subpattern if
the entire argument is a string value.)

3. You are not matching that which you intend to match.
(You are only matching a single hexadecimal digit.)

This should work:

const generateHex2Rgba =
((alpha = 1) => ((hex, red, green, blue, count) => {
let [r, g, b] = [red, green, blue].map(s => parseInt(s, 16));
return `rgba(${r},${g},${b},${alpha})`;
}));

str = str.replace(
/#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/gi,
generateHex2Rgba()
);

Why you want to do that remains a mystery, though.

--
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

Re: Help with regexp

<3142540.44csPzL39Z@PointedEars.de>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17219&group=comp.lang.javascript#17219

  copy link   Newsgroups: comp.lang.javascript
Path: i2pn2.org!i2pn.org!aioe.org!news.mb-net.net!open-news-network.org!.POSTED.178.197.212.81!not-for-mail
From: PointedEars@web.de (Thomas 'PointedEars' Lahn)
Newsgroups: comp.lang.javascript
Subject: Re: Help with regexp
Date: Sat, 07 Aug 2021 22:02:12 +0200
Organization: PointedEars Software (PES)
Lines: 19
Message-ID: <3142540.44csPzL39Z@PointedEars.de>
References: <lZadnd6QivChfpH8nZ2dnUU7-L3NnZ2d@westnet.com.au> <3136550.aeNJFYEL58@PointedEars.de>
Reply-To: Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Mime-Version: 1.0
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 7Bit
Injection-Info: gwaiyur.mb-net.net; posting-host="178.197.212.81";
logging-data="15407"; mail-complaints-to="abuse@open-news-network.org"
User-Agent: KNode/4.14.10
Cancel-Lock: sha1:xqz+j00bfPzG51a+dAjogiG8a1E=
X-User-ID: U2FsdGVkX18omHTJTiyNhUnL8q0zD63n3R9kPcy/ZJYOwpjxM4n7tw==
Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
 by: Thomas 'Pointed - Sat, 7 Aug 2021 20:02 UTC

Thomas 'PointedEars' Lahn wrote:

> const generateHex2Rgba =
> ((alpha = 1) => ((hex, red, green, blue, count) => {

I actually recommend that it be written

alpha=1

instead to tell apart the optional argument, and default value of the formal
parameter, from a standard assignment statement.

[Of course, optimizations are possible. For example, having the pattern
outside the callback-generating function is a violation of DRY.]

--
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

Re: Help with regexp

<F9SdnRF4Svb6AI38nZ2dnUU7-TPNnZ2d@westnet.com.au>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17220&group=comp.lang.javascript#17220

  copy link   Newsgroups: comp.lang.javascript
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.snarked.org!border2.nntp.dca1.giganews.com!nntp.giganews.com!buffer2.nntp.dca1.giganews.com!nntp.westnet.com.au!news.westnet.com.au.POSTED!not-for-mail
NNTP-Posting-Date: Sun, 08 Aug 2021 22:13:43 -0500
Subject: Re: Help with regexp
Newsgroups: comp.lang.javascript
References: <lZadnd6QivChfpH8nZ2dnUU7-L3NnZ2d@westnet.com.au>
<3136550.aeNJFYEL58@PointedEars.de>
From: ap_prog@hotmail.com (Andrew Poulos)
Date: Mon, 9 Aug 2021 13:13:40 +1000
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101
Thunderbird/78.12.0
MIME-Version: 1.0
In-Reply-To: <3136550.aeNJFYEL58@PointedEars.de>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Language: en-AU
Content-Transfer-Encoding: 8bit
Message-ID: <F9SdnRF4Svb6AI38nZ2dnUU7-TPNnZ2d@westnet.com.au>
Lines: 62
X-Usenet-Provider: http://www.giganews.com
NNTP-Posting-Host: 110.175.24.76
X-Trace: sv3-3qC+pgIdow1NySn6h2oSYJqPxmnQeSkVJ3rb0awcUm6/mmybZG65o26/iZ3ebdf+/JjymCTbaQjnpA7!4TD7difs/TdNaPpQZm4S4cPQ5J0+Qzo1nxrVRrPyFk3BmFNdTr9+A2RlQTStyCPilX9IKL9WqxNq!1e/Zm7g2wmb0O1iC0Llg
X-Complaints-To: abuse@westnet.com.au
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 3252
 by: Andrew Poulos - Mon, 9 Aug 2021 03:13 UTC

On 8/08/2021 5:56 am, Thomas 'PointedEars' Lahn wrote:
> Andrew Poulos wrote:
>
>> I have a string that looks like this
>>
>> let str = 'linear-gradient(90deg, #ff0000 50%, #00ff00 100%)"
>>
>> I need to convert the hex values to rgba so I've written a function
>>
>> const convertHex2Rgba = (hex, alpha = 1) => {
>> ...
>> return `rgba(${r},${g},${b},${alpha})`;
>> };
>>
>> I'm stuck trying to write a regexp that replaces the hex values (that
>> may have either 3 or 6 characters - plus the #) with the value returned
>> from convertHex2Rgba.
>>
>> This (one of my many failed attempts):
>>
>> str.replace(/#[0-9a-fA-F]/, convertHex2Rgba('$1'));
>>
>> does nothing.
>
> Of course not.
>
> 1. You are not using the return value of the “replace” method.
>
> 2. You are not passing your function a proper value for the formal
> parameter “hex”, but the string value '$1' instead.
> ('$1' is only expanded with the first captured subpattern if
> the entire argument is a string value.)
>
> 3. You are not matching that which you intend to match.
> (You are only matching a single hexadecimal digit.)
>
> This should work:
>
> const generateHex2Rgba =
> ((alpha = 1) => ((hex, red, green, blue, count) => {
> let [r, g, b] = [red, green, blue].map(s => parseInt(s, 16));
> return `rgba(${r},${g},${b},${alpha})`;
> }));

Nice and concise code but what is 'count' for?

> str = str.replace(
> /#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})/gi,
> generateHex2Rgba()
> );
>
> Why you want to do that remains a mystery, though.

The hex values are from gradient backgrounds that users are grabbing
from online CSS generators (most, if not all, of them return colours in
hex). The backgrounds need to animate so I'm using anime.js to "play"
with the various colour and alpha values. This is easier for me with
rgba than (hex or hexa).

Andrew Poulos

Re: Help with regexp

<21167627.EfDdHjke4D@PointedEars.de>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17221&group=comp.lang.javascript#17221

  copy link   Newsgroups: comp.lang.javascript
Path: i2pn2.org!i2pn.org!news.niel.me!aioe.org!news.mb-net.net!open-news-network.org!.POSTED.178.197.212.81!not-for-mail
From: PointedEars@web.de (Thomas 'PointedEars' Lahn)
Newsgroups: comp.lang.javascript
Subject: Re: Help with regexp
Date: Tue, 10 Aug 2021 13:45:04 +0200
Organization: PointedEars Software (PES)
Lines: 50
Message-ID: <21167627.EfDdHjke4D@PointedEars.de>
References: <lZadnd6QivChfpH8nZ2dnUU7-L3NnZ2d@westnet.com.au> <3136550.aeNJFYEL58@PointedEars.de> <F9SdnRF4Svb6AI38nZ2dnUU7-TPNnZ2d@westnet.com.au>
Reply-To: Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 8Bit
Injection-Info: gwaiyur.mb-net.net; posting-host="178.197.212.81";
logging-data="3578"; mail-complaints-to="abuse@open-news-network.org"
User-Agent: KNode/4.14.10
Cancel-Lock: sha1:JKkyePCcnD/PH7E1sj+ytxurXHo=
X-User-ID: U2FsdGVkX19ZaaE58k9CvyT2cDMy+Ae4sT8X6fP7bXu0+iKWTVcCCg==
X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
 by: Thomas 'Pointed - Tue, 10 Aug 2021 11:45 UTC

Andrew Poulos wrote:

> On 8/08/2021 5:56 am, Thomas 'PointedEars' Lahn wrote:
>> const generateHex2Rgba =
>> ((alpha = 1) => ((hex, red, green, blue, count) => {
>> let [r, g, b] = [red, green, blue].map(s => parseInt(s, 16));
>> return `rgba(${r},${g},${b},${alpha})`;
>> }));
>
> Nice and concise code

Thank you.

> but what is 'count' for?

It should have better been named “offset”, and apparently is only an
artifact of code from my memory of more general code that I had written
before.

The formal parameter that follows the formal parameters for the captures
(here: “red”, “green”, and “blue”) would be assigned the offset of the
match, and the formal parameter that followed that would be assigned the
original string value:

<https://262.ecma-international.org/#sec-string.prototype.replace>

[Do you also yearn for the days when the ECMAScript Language Specification
was written in formal language, yes, and it wasn’t clickable, but at
least it was not completely shrouded in abstractions?

I for one cannot see anymore how that describes the (actual) behavior of
the “replace” method in this case. Where is the check for RegExp?
Or are we supposed to use “replaceAll” now?
(Should I just have brunch first instead?)]

I must have misremembered that it this formal parameter would need to be
specified in order for the capture parameters be assigned the correct value,
as I cannot produce by testing, or find in documentation, supporting
evidence now.

Specifically the following historical example indicates that there is no
backwards compatibility issue, and that it can be safely omitted in this
case:

<http://web.archive.org/web/20040929130418/http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/string.html#1194258>

--
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

Re: Help with regexp

<slrnsh4s8a.49p.jon+usenet@raven.unequivocal.eu>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17222&group=comp.lang.javascript#17222

  copy link   Newsgroups: comp.lang.javascript
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jon+usenet@unequivocal.eu (Jon Ribbens)
Newsgroups: comp.lang.javascript
Subject: Re: Help with regexp
Date: Tue, 10 Aug 2021 12:28:58 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 27
Message-ID: <slrnsh4s8a.49p.jon+usenet@raven.unequivocal.eu>
References: <lZadnd6QivChfpH8nZ2dnUU7-L3NnZ2d@westnet.com.au>
<3136550.aeNJFYEL58@PointedEars.de>
<F9SdnRF4Svb6AI38nZ2dnUU7-TPNnZ2d@westnet.com.au>
<21167627.EfDdHjke4D@PointedEars.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Tue, 10 Aug 2021 12:28:58 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="f10d4714e942055326eee7aab3c969d5";
logging-data="20501"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/aM/SiF2R5zw3kR21cPHxQHe7Zc2bumcE="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:sh09KP9IKEUxITVlQQBedA6OG20=
 by: Jon Ribbens - Tue, 10 Aug 2021 12:28 UTC

On 2021-08-10, Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote:
><https://262.ecma-international.org/#sec-string.prototype.replace>
>
> [Do you also yearn for the days when the ECMAScript Language Specification
> was written in formal language, yes, and it wasn’t clickable, but at
> least it was not completely shrouded in abstractions?
>
> I for one cannot see anymore how that describes the (actual) behavior of
> the “replace” method in this case. Where is the check for RegExp?
> Or are we supposed to use “replaceAll” now?
> (Should I just have brunch first instead?)]

Step 2:

If searchValue is neither undefined nor null, then
a. Let replacer be ? GetMethod(searchValue, @@replace).
b. If replacer is not undefined, then
i. Return ? Call(replacer, searchValue, « O, replaceValue »).

RegExp ojects have an @@replace method so that takes over the
handling of the call. Interesting to know that you can presumably
therefore create your own classes with @@replace methods to also
affect the String replace method.

The abstractions in the ECMAScript standard aren't *too* bad I think.
The effort required to find out what's happening in any particular
situation doesn't normally seem to end up being overly burdensome.

Re: Help with regexp

<7288015.EvYhyI6sBW@PointedEars.de>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17223&group=comp.lang.javascript#17223

  copy link   Newsgroups: comp.lang.javascript
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.mb-net.net!open-news-network.org!.POSTED.178.197.212.81!not-for-mail
From: PointedEars@web.de (Thomas 'PointedEars' Lahn)
Newsgroups: comp.lang.javascript
Subject: Re: Help with regexp
Date: Wed, 11 Aug 2021 16:03:15 +0200
Organization: PointedEars Software (PES)
Lines: 39
Message-ID: <7288015.EvYhyI6sBW@PointedEars.de>
References: <lZadnd6QivChfpH8nZ2dnUU7-L3NnZ2d@westnet.com.au> <3136550.aeNJFYEL58@PointedEars.de> <F9SdnRF4Svb6AI38nZ2dnUU7-TPNnZ2d@westnet.com.au> <21167627.EfDdHjke4D@PointedEars.de> <slrnsh4s8a.49p.jon+usenet@raven.unequivocal.eu>
Reply-To: Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 8Bit
Injection-Info: gwaiyur.mb-net.net; posting-host="178.197.212.81";
logging-data="4368"; mail-complaints-to="abuse@open-news-network.org"
User-Agent: KNode/4.14.10
Cancel-Lock: sha1:6Yo5SiP633AYd6Lx5ZiYVS6hZ/U=
Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
X-User-ID: U2FsdGVkX18DNmcqwiGMck7JDkPXMXrpe2vO3ahewSdrdYLdavyneQ==
X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
 by: Thomas 'Pointed - Wed, 11 Aug 2021 14:03 UTC

Jon Ribbens wrote:

> On 2021-08-10, Thomas 'PointedEars' Lahn <PointedEars@web.de> wrote:
>><https://262.ecma-international.org/#sec-string.prototype.replace>
>> [… I for one cannot see anymore how that describes the (actual) behavior
>> of the “replace” method in this case. Where is the check for RegExp?
>> Or are we supposed to use “replaceAll” now?
>
> Step 2:
>
> If searchValue is neither undefined nor null, then
> a. Let replacer be ? GetMethod(searchValue, @@replace).
> b. If replacer is not undefined, then
> i. Return ? Call(replacer, searchValue, « O, replaceValue »).
>
> RegExp ojects have an @@replace method so that takes over the
> handling of the call. Interesting to know that you can presumably
> therefore create your own classes with @@replace methods to also
> affect the String replace method.

There it is:

<https://262.ecma-international.org/#sec-regexp.prototype-@@replace>

Thank you very much ':-)

> The abstractions in the ECMAScript standard aren't *too* bad I think.
> The effort required to find out what's happening in any particular
> situation doesn't normally seem to end up being overly burdensome.

I have been interpreting them since Edition 3 Final was published at
the latest, and in my opinion it gets worse with every new Edition :'-)

PointedEars
--
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.


devel / comp.lang.javascript / Re: Help with regexp

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor