skip to Main Content

I get back a histogram array from Photoshop as a string

 var rArray = activeDocument.channels["Red"].histogram.toString()

For those of you without Photoshop, just ignore that detail as it’ll be an array something like

66500,0,0,0,0,0,0,0,0,0,10000,0,0,0,0,0,0,0...
0,0,0,0,0,0,0,0,750,0,0,0,0,0,0,0,0,0,0...

Currently I’ve got a function to loop over the (string as an) array and remove any zero values. However, I’m sure it can be done with a regex. Only I’m picking up one of the zeros of of the number I want to keep.

.replace(/(,0)/g,"")

only that returns

665010000
75

instead of

66500
750

The expected output is to eliminate all zeros

66500,10000
750

See it here on regex101

I’m pretty sure this can me done as regex only, I don’t know how to specify "0," OR "0$" zero AND a comma OR zero and ENDSTRING literally.

2

Answers


  1. The pattern /^0|,0/g should do the trick.

    check here:
    regexr.com/5akbq

    Login or Signup to reply.
  2. You can use

    ^(?:0,)+|(?:,0)+$|b0,
    

    See the regex demo

    Details

    • ^(?:0,)+ – one or more occurrences of 0, substring from the string start
    • | – or
    • (?:,0)+$ – one or more occurrences of ,0 substring till the string end
    • | – or
    • b0, – a word boundary followed with 0, substring.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search