Copy link to clipboard
Copied
// constraints are:
// XX T will always be first and in caps ==> ^XX T
// T will always have a value of max of 2 digits ==> {1,2}
// L may or may not be present but if present will always have a value of max of 2 digits ==> {1,2}
// the digits of L if present will always be the end of the string ==> (/\d+$/)
var testName1 = "XX T3 L22"; // viable upper case search(/^XX T\d{1,2}( L\d{1,2}$)/g)
var testName2 = "XX T44 L1";
var testName3 = "XX T5"; // viable upper case search(/^XX T\d{1,2}$/g)
var testName4 = "XX T66;
// for testName1 ==> lNumber1 = 22
Hi @nutradial, I assume you are doing this via ExtendScript... see if this helps. The match method of string is great for this.
- Mark
var tests = [
"XX T3 L22",
"XX T44 L1",
"XX T5",
"XX T66"
];
// match two digits of T and two digits of L (if L exists)
var matcher = /^XX T(\d{1,2})(?: L(\d{1,2}))?/;
for (var i = 0, T, L; i < tests.length; i++) {
// perform the regex match
var match = tests[i].match(matcher);
if (!match)
// no match at all
conti
...
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Hi @nutradial, I assume you are doing this via ExtendScript... see if this helps. The match method of string is great for this.
- Mark
var tests = [
"XX T3 L22",
"XX T44 L1",
"XX T5",
"XX T66"
];
// match two digits of T and two digits of L (if L exists)
var matcher = /^XX T(\d{1,2})(?: L(\d{1,2}))?/;
for (var i = 0, T, L; i < tests.length; i++) {
// perform the regex match
var match = tests[i].match(matcher);
if (!match)
// no match at all
continue;
T = match.length > 0 ? Number(match[1]) : undefined;
L = match.length > 1 ? Number(match[2]) : undefined;
$.writeln('test string "' + tests[i] + '": T = ' + T + ' L = ' + L);
}
Copy link to clipboard
Copied
Nice!!! Thanks.
Copy link to clipboard
Copied
You're welcome!
Copy link to clipboard
Copied
Can the match method as implemented above be used outside an object?
I am already iterating layer.name
Copy link to clipboard
Copied
All you need is a String and a RegExp. You can call the "match" method of any String, passing a RegExp as parameter. Documentation is here. It returns null if no match, or an array where the zeroth element is the whole string, and subsequent elements are captured from the RegExp parentheses.
Copy link to clipboard
Copied
To me the best part of this post is how you structured the regex to return the T and L value. I appreciate it ...I learned something very useful.
Copy link to clipboard
Copied
Yes! Regex is the most amazing thing! Well worth taking the time to understand.
Copy link to clipboard
Copied
This might be off topic and I'll gladly create a new thread if need be:
Copy link to clipboard
Copied
I was able to make it work using:
if ((!T) && layer.name !== ""). // for T is NaN
if ((!!T) && layer.name !== ""). // for T has value
Copy link to clipboard
Copied
You can test for NaN ...
if (isNaN(T))
Also be careful about using !T this will return true if T is zero. When working with numbers it might be better to use something like if (undefined == T) . Depends on your specific case, but as a general rule I don't use that when working with numbers, only objects.
Copy link to clipboard
Copied
I added a third parameter to my search pattern and was wondering if "named capturing groups" in ExtendScript are fully supported?
XX T7 L3 D0.125 ==> Pattern match 1 (XX T7) Pattern match 2 (L3) Pattern match 3 (D0.125)
/(?<t>^XX T(\d{1,2}))?(?<l> ?L(\d{1,2}))?(?<d> ?D(\d*\.?\d{1,3}))?/g
Examples of matches:
XX T7
XX T7 L3
XX T7 D0.125
XX T7 L3 D0.125
XX T7 D0.125 L3
L3
D0.125
L3 D0.125
D0.125 L3
Named capturing groups:
t = 7
l = 3
d = 0.125
If named capturing groups are viable can you please update your initial snippet of code for reference. I am using the search and match method to trigger events based on layer names. This thread has not only been of high utility but very educational. Thanks.
Copy link to clipboard
Copied
Unfortunately ExtendScript RegExp doesn't support named capture groups. The best we can do is use a non-capturing group as I did in my original code, so that only the relevant patterns are matched..
/^XX T(\d{1,2})(?:\s?L(\d{1,2}))?(?:\s?D(\d*\.?\d{1,3}))?/