Вопрос

         var layerName = layer.name;
         //replace weird characters in layer names
         var layerName = layerName.replace(")", "-");
         var layerName = layerName.replace("(", "-");
         var layerName = layerName.replace(":", "-");
         var layerName = layerName.replace(":", "-");
         var layerName = layerName.replace(/\//, "-");
         var layerName = layerName.replace('/', "");
         var layerName = layerName.replace("---", "-");
         var layerName = layerName.replace("--", "-");
         var file = new File(folder.fsName+"/"+layerName+".png");

using extendscript but for some reason it wont replace two "--" characters. I feel like I could do this easier with regex but I suck at those. am trying to replace all weird non alpha-numeric char and spaces with "-"

Это было полезно?

Решение

How about this:


// Test String
var layerName = "123---A%*^%   )()H";
layerName
     .replace(/\-+/g,'-')  // Collapse multiple dashes into a single one 
     .replace(/\W+/g,'-'); // Replace Non-word characters

Result:

123-A-H

\W+ Matches all non-word characters,

Другие советы

The regex you want is:

[^a-zA-Z0-9] 

and your replace character is '-'

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top