0
         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 "-"

1
  • 1
    you only need to declare the layerName variable once Commented Jan 18, 2012 at 20:38

2 Answers 2

2

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,

Sign up to request clarification or add additional context in comments.

2 Comments

this worked perfect. thanks! I will have to start diving into regex a lot more.
You're welcome, heres a quick link for regexes, I learned a lot in that website Regular Expresions
0

The regex you want is:

[^a-zA-Z0-9] 

and your replace character is '-'

1 Comment

like this? var layerName = layerName.replace("[^a-zA-Z0-9]", "-"); it doesn't seem to work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.