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


Groups > comp.lang.javascript > #40331

Re: Help with regexp

From The Natural Philosopher <tnp@invalid.invalid>
Newsgroups comp.lang.javascript
Subject Re: Help with regexp
Date 2021-08-06 19:10 +0100
Organization A little, after lunch
Message-ID <sejtu5$59s$9@dont-email.me> (permalink)
References <lZadnd6QivChfpH8nZ2dnUU7-L3NnZ2d@westnet.com.au>

Show all headers | View raw


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.

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