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


Groups > comp.lang.javascript > #40337

Re: Help with regexp

Path csiph.com!weretis.net!feeder8.news.weretis.net!news.mb-net.net!open-news-network.org!.POSTED.178.197.212.81!not-for-mail
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
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> (permalink)
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==
Xref csiph.com comp.lang.javascript:40337

Show key headers only | View raw


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.

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


Thread

Help with regexp Andrew Poulos <ap_prog@hotmail.com> - 2021-08-06 17:22 +1000
  Re: Help with regexp Andrew Poulos <ap_prog@hotmail.com> - 2021-08-06 17:41 +1000
    Re: Help with regexp Andrew Poulos <ap_prog@hotmail.com> - 2021-08-06 17:55 +1000
      Re: Help with regexp Julio Di Egidio <julio@diegidio.name> - 2021-08-06 01:39 -0700
        Re: Help with regexp Julio Di Egidio <julio@diegidio.name> - 2021-08-06 01:53 -0700
        Re: Help with regexp Andrew Poulos <ap_prog@hotmail.com> - 2021-08-07 09:19 +1000
          Re: Help with regexp Julio Di Egidio <julio@diegidio.name> - 2021-08-06 17:26 -0700
      Re: Help with regexp Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-08-06 10:36 +0000
        Re: Help with regexp Andrew Poulos <ap_prog@hotmail.com> - 2021-08-07 09:17 +1000
          Re: Help with regexp Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-08-07 18:57 +0000
  Re: Help with regexp The Natural Philosopher <tnp@invalid.invalid> - 2021-08-06 19:10 +0100
  Re: Help with regexp Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-08-07 21:56 +0200
    Re: Help with regexp Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-08-07 22:02 +0200
    Re: Help with regexp Andrew Poulos <ap_prog@hotmail.com> - 2021-08-09 13:13 +1000
      Re: Help with regexp Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-08-10 13:45 +0200
        Re: Help with regexp Jon Ribbens <jon+usenet@unequivocal.eu> - 2021-08-10 12:28 +0000
          Re: Help with regexp Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2021-08-11 16:03 +0200

csiph-web