0

I need to replace a XXXXXX sequence in my document with the same number of underscore characters. I'm using (Y{2,15}) as the find argument, but can´t find out what is the correct code for the replace argument.

2
  • 1
    InDesign's GREP does not support that. (Nor, so I believe, do other common dialects of GREP.) You could write a script, though, as it does allow examining the "found" contents' length.
    – Jongware
    Jan 20, 2018 at 15:25
  • 1
    Is it specifically between 2 and 15 X’s? Is there any possibility of the document containing strings of more than 15 X’s in a row which you don’t want to change? Jan 20, 2018 at 22:29

3 Answers 3

3

Indeed a script can do it:

//Main routine
var main = function() {

//==================VARS==================//
var 
//The document
doc = app.properties.activeDocument,
//Find Grep Preferences in their current state
fgp = app.findGrepPreferences.properties,
//variables used for storing grep find values and items
found, n, text, count,
//A simple "underscores" string to replace found items contents
underscores = "___________________________________________________________________________________________________________";


//==================INITIAL CHECK==================//
if ( !doc ) return;

//==================SETTING GREP PROPS==================//
app.findGrepPreferences.properties = {
    findWhat:"X{2,15}"
}

//==================FINDING STRINGS==================//
found = doc.findGrep();
n = found.length;
count = n;

//==================EXIT IF NO RESULTS==================//
if ( !n ) {
    alert("No results sorry !");
    return;
}

//==================REPLACING STRINGS==================//
while ( n-- ) {
    text = found[n];
    text.contents = underscores.substr (1, text.length);
}

//==================RESTORING GREP PREFS==================//
app.findGrepPreferences.properties = fgp;

//==================ALERT AND EXIT==================//
alert( count+" strings replaced" );
}
var u;
app.doScript ( "main()",u,u,UndoModes.ENTIRE_SCRIPT, "The Script" );

[Initial situation Strings replaced !

2
  • 1
    Suggestion: use text.contents = text.contents.replace(/X/g, '_'); to get rid of that string of underlines.
    – Jongware
    Jan 21, 2018 at 19:25
  • @usr2564301 yeah you are right, dunno why I didn't do it this way :P
    – Loic
    Jan 22, 2018 at 13:43
1

Not possible, however you can do it in several passes each replacing one character. You have to run the search and replace 15 times though. Might as well do this with scripting.

0

Use a GREP Style. A GREP Style has the advantages that it is not destructive (it does not change your document contents), it is live (the style will automatically be removed and applied while you are changing your text), and when set up correctly it uses the exact same width as the original text (so there is no chance of unexpectedly changing your text flow).

An additional advantage is that you also can change the appearance of the underline itself: you can change its color, make it thinner or thicker, move it higher or lower, and make it a double, dashed, or dotted line. To do so, you can change the Underline Options in the character style.

  1. Create a character style "Underline". Set its character color to 'None', switch Underline to On, and its color to Black (as the default is 'text color').

new character style

  1. Add a GREP Style to your base text style that applies the character style to the match sequence (?<!X)X{2,15}(?!X).

paragraph style options

  1. Done.

The GREP match ensures that only an exact sequence of 2 to 15 X's in a row change to an underline. This exact match ensures that a sequence of 16 X's will not change to 15 underlines plus a single "X"; they will all remain unchanged. Also, using your original GREP, a sequence of 17 X's will be changed – first 15 in a row, then 2 in the next match. Again, my more exact match will not change such a sequence.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.