variable parsing into regex - what is the trick?

Find out why the . goes before the /

Moderator: Paul Tuersley

Post Reply
everydayme
Posts: 4
Joined: August 13th, 2008, 9:33 pm

I have been trying to figure this out for hours. What is the trick to getting a variable into a regular expression:

(regular expression searching for string: fur)
(full code below)

this works fine:

Code: Select all

var itemIndexifReturned = (layer.search(/fur/gi,""));
alert (itemIndexifReturned);
but when I try to parse a variable it doesn't work:

Code: Select all

var searchStringer = '/fur/gi';
var itemIndexifReturned = (layer.search(searchStringer,""));
alert (itemIndexifReturned);
I have tried a variety of searchStringer's like the var having just the string ('fur'), and then

Code: Select all

.search(/searchStringer/gi,"")
and also

Code: Select all

.search(/+searchStringer+/gi,"")
and

Code: Select all

.search("/"+searchString+"/gi","")
but to no avail - am I missing something?

whole code here:
(this is the line you can change to get it to work:)
var itemIndexifReturned = (layer.search(searchStringer,""));
to
var itemIndexifReturned = (layer.search(/fur/gi,""));

Code: Select all

// code will eventually loop all layers in current comp, then we can do something to each layer that has a matching search string
//the script will ask user for search string, like fur, occ, bg, col etc...
// for the sake of other peoples searches... this script remembers (stores) the last input and recalls and inserts it the next time the script is run.

var title = ("color layers by");
var firstVal = "Fur";
if (app.settings.haveSetting("jono", title)==false) app.settings.saveSetting("jono", title, firstVal);

var layers = app.project.activeItem.layers;                        // Retrieve the composition's layers
var layer;

//var searchString = prompt("Search by:",app.settings.getSetting("jono", title),title);
//if (searchString != null) app.settings.saveSetting("jono", title, searchString);

var searchStringer = '/fur/gi';
//alert (searchStringer);

for (var i = 1; i <= layers.length; i++) {
    layer = layers[i].name;
    alert(layer);
    
    //alert((layer).match(searchStringer)[1]);
    var itemIndexifReturned = (layer.search(searchStringer,"")); 
    alert(itemIndexifReturned);
    
    //var itemIndexifReturned = (searchStringer.search(layer))
    if (itemIndexifReturned!= -1)   {
            layers[i].blendingMode = BlendingMode.MULTIPLY; // test if working
        }
}
code not cleaned yet :)
beginUndoGroup
Posts: 81
Joined: November 27th, 2012, 6:41 am

A RegExp is not a string. Try replacing var searchStringer = '/fur/gi'; by var searchStringer = /fur/gi;
everydayme
Posts: 4
Joined: August 13th, 2008, 9:33 pm

Darn it, so simple! Thanks for taking the time to help beginundogroup!

Cheers to your raise! :)
Post Reply