Skip to main content
Inspiring
Oct 15, 2025 11:39 PM UTC
Answered

Anybody know why this grep regex doesn't work?

I'm trying to find periods with zero or more whitespace characters after them. Everything I can find says this should work, but it doesn't work for strings that have no whitespace (looking for a capital letter followed by a period):

 

var capitalExpression = "^[A-Z]\\.\\s*";
 
This finds
A. Start the engine.
 
but not
A.Start the engine.
rob day
rob dayCorrect answer
Community Expert
Oct 16, 2025 12:42 PM UTC

Hi @Thomas_Calvin , It’s also working for me with ExtendScript. Here it is via a function:

 

grepSearch("^[A-Z]\\.\\s*", "B. ")

/**
* Document Grep find and change 
* @ param f the find grep string 
* @ param c the change grep string
* @ return void 
*/
function grepSearch(f,c){
    app.findGrepPreferences = app.changeGrepPreferences = app.findChangeGrepOptions = null;
    app.findChangeGrepOptions.properties = app.findChangeGrepOptions.properties = {includeFootnotes:true, includeHiddenLayers:true, includeMasterPages:true, wholeWord:false}  
    app.findGrepPreferences.findWhat = f;
    app.changeGrepPreferences.changeTo = c;
    app.activeDocument.changeGrep( true )
}

 

 

 

15 replies

rob day
Community Expert
rob dayCommunity ExpertCorrect answer
Community Expert
Oct 16, 2025 12:42 PM UTC

Hi @Thomas_Calvin , It’s also working for me with ExtendScript. Here it is via a function:

 

grepSearch("^[A-Z]\\.\\s*", "B. ")

/**
* Document Grep find and change 
* @ param f the find grep string 
* @ param c the change grep string
* @ return void 
*/
function grepSearch(f,c){
    app.findGrepPreferences = app.changeGrepPreferences = app.findChangeGrepOptions = null;
    app.findChangeGrepOptions.properties = app.findChangeGrepOptions.properties = {includeFootnotes:true, includeHiddenLayers:true, includeMasterPages:true, wholeWord:false}  
    app.findGrepPreferences.findWhat = f;
    app.changeGrepPreferences.changeTo = c;
    app.activeDocument.changeGrep( true )
}

 

 

 

Translate
Inspiring
Oct 16, 2025 5:17 PM UTC

Thanks Rob.

In the end, it works fine. The UXP developer tool did not pick up my source-code changes for some reason (despite "watch" being on), and was running an old version that didn't have the asterisk.

Translate
Peter Kahrel
Community Expert
Community Expert
Oct 16, 2025 8:27 PM UTC

Yes, you found out the hard way that the UXP developer tool can't save changes you made to the code. It's pretty awful, but that's the way it is.

Translate
Community Expert
Oct 16, 2025 6:37 AM UTC

Noticed you tagged 'UXP Scripting' - are you writing a UXP script? 

It might need to be something like this

/^[A-Z]\.\s*/
Translate
Inspiring
Oct 16, 2025 5:16 PM UTC

It is a UXP script. A plug-in, in fact.

Translate
Joel Cherney
Community Expert
Community Expert
Oct 16, 2025 3:25 AM UTC

I don't think that the problem is with your query. Maybe something else is going on somewhere else in your script? Your query seems to work for me, at least when I put it in the simplest script possible:

 

 

 

Translate
Inspiring
Oct 16, 2025 5:19 PM UTC

Yep, thanks. It turns out that it works fine for me too. The UXP developer tool neglected to reload the source after I changed and saved it, despite "watch" being on. Usually this works fine. I noticed the wrong expression being used when stepping through with the debugger.

Translate
brian_p_dts
Community Expert
Community Expert
Oct 16, 2025 1:46 AM UTC

The ens should be \\s+? I think

Translate
Inspiring
Oct 16, 2025 1:49 AM UTC

Thanks for the reply. Everything I've looked at says + means "one or more" not zero or more.

Translate
Barb Binder
Community Expert
Community Expert
Oct 16, 2025 1:02 AM UTC

Oh darn. I focused on your GREP expression as per your title, and missed that you need this included in a script. Not sure that my answer will be helpful. Sorry!

 

~Barb

Translate
~Barb at Rocky Mountain Training
Inspiring
Oct 16, 2025 1:48 AM UTC

I appreciate it Barb! I have a working script and a mostly-working expression. But the asterisk doesn't work.

Translate
Barb Binder
Community Expert
Community Expert
Oct 16, 2025 1:00 AM UTC

How about:

 

\u\.\s*

 

 

~Barb

Translate
~Barb at Rocky Mountain Training