Skip to main content
Participating Frequently
May 25, 2021
Answered

Illustrator Regular Expression - Negative Lookbehind??

  • May 25, 2021
  • 2 replies
  • 1575 views

I am trying to use regex to find any digits except if they are preceded by "_" underscore symbol.  If they are preceded by underscore it should not match.  I have gotten this to work in RegEx testing but it looks as though negative lookbehind is not supported: 

 

var reg = /(?<![_\d+])\d+/g;

 

Is there a way to accomplish this without negative lookbehind?

The regex above should return the digits in NAME12 but not NAME_12

 

Thanks in advance.

This topic has been closed for replies.
Correct answer CarlosCanto

how about removing "_digits" first? would that work for you?

 

var str = "NAME14 but not NAME_12 and NAME23";

var re = /\d+/g;

var arr = str.replace(/_\d+/g, '').match(re);

for (var a=0; a<arr.length; a++) {
    alert(arr[a]);
}

2 replies

Disposition_Dev
Legend
May 26, 2021

try this:

 

[^_]+[\d]{2}

 

that's any character that's not an underscore at least once, followed by 2 digits.

Participating Frequently
May 26, 2021

Thank you for your reply.  Please see my response to Carlos above for hopefully a more detailed description.  This regex gives me the following with my example text:

 

NAME12  NAME_12

 

I am changing the color of this range then performing other changes to the full string.  This is what I am trying to acheive:

 

NAME12  NAME_12

 

Also note, there may be more than two digits.  Example:  NAME1234  NAME_1234

Silly-V
Legend
May 26, 2021

Use a non-capturing group:

 

  var str = "NAME_1234";
  var rx = /(?:[^_])+\d+/;
  alert(str.match(rx)); // "1234"
CarlosCanto
Community Expert
CarlosCantoCommunity ExpertCorrect answer
Community Expert
May 26, 2021

how about removing "_digits" first? would that work for you?

 

var str = "NAME14 but not NAME_12 and NAME23";

var re = /\d+/g;

var arr = str.replace(/_\d+/g, '').match(re);

for (var a=0; a<arr.length; a++) {
    alert(arr[a]);
}
Participating Frequently
May 26, 2021

Thank you for your reply.  This would work, however, I am using the full string later on so removing that isn't ideal.  I am using this regex to make changes (color) to this selected range (any number in the string not directly following an underscore, in this case the underscore is treated as a space).  The color change should only happen on numbers that are not preceded by "_".  There will be other changes that are made to the full string later on as well.  So far I have not been able to get anything to work in ExtendScript.  The negative lookbehind is exactly what I need the regex to do but is unsupported for Adobe.

 

Below is how two different examples should be after this regex + color change:

NAME12 but not NAME_12

Disposition_Dev
Legend
May 26, 2021

Carlos' answer doesn't preclude using the full string later. It doesn't update the string or change it in any way. It's creating a new variable and storing an updated string in that variable. The original string is left untouched so that it could be referenced again later.

 

Can you give a more thorough example of what your string(s) will look like? Is it a long string with lots of stuff in it that might have several instances of numbers that need to be reformatted (like carlos' example above)? Or is it a bunch of short strings that will only have one instance that needs to be reformatted (like a bunch of text frames that say "NAME12" and "NAME26" and "NAME_45")? This kind of information is very helpful in terms of determining a plan of action.