• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

question mark in javascript/grep expression

Explorer ,
Jan 31, 2017 Jan 31, 2017

Copy link to clipboard

Copied

Hello!

I've got problem with question mark character in my code. When I'm trying to use it in expression such as '[!?] \l' (find any '?' or '!' fallowed by lowercase letter) it gives my only those strings with '!', but not with '?'.

The same time there is no problem with other kinds of expressions with '?' character. It seems that placing it into a square parentheses causes the problem.

I've tried to use escape characters - '\', double '\\', even '/'; but without result.

Where is the problem?

Thanks in advance

jf

TOPICS
Scripting

Views

2.9K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advisor , Feb 15, 2017 Feb 15, 2017

Stepping through your code it's fairly easy to understand the mistake.

Let's use the following input text as example:

ala bala? portocala

ala bala. portocala

ala bala! portocala

Your initial grep search ('[.!?] \\l') works just fine, and it returns an 3 element array:
? p. p

! p

Then those three finds are used to create a new grep search, and I hope you can now already see the issue:

instead of searching for '\\? p' you are searching for just '? p' where the '?' is considered a regex special character (e

...

Votes

Translate

Translate
People's Champ ,
Jan 31, 2017 Jan 31, 2017

Copy link to clipboard

Copied

This should work:

[!\?]\l

if as a string, double the backslash

"[!\\?]\\l"

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 31, 2017 Jan 31, 2017

Copy link to clipboard

Copied

Here is my code:

function fnFindChange(findProperties, changeProperties, applySearchTo) {

    app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;

    includeFootnotes = true;

    includeHiddenLayers = false;

    includeLockedLayersForFind = false;

    includeLockedStoriesForFind = false;

    includeMasterPages = false;

    app.findGrepPreferences.properties = findProperties;

    app.changeGrepPreferences.properties = changeProperties;

    applySearchTo.changeGrep();

    app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;

};

function fnFindGrep(findProperties, applySearchTo) {

    app.findGrepPreferences = NothingEnum.nothing;

    includeFootnotes = true;

    includeHiddenLayers = false;

    includeLockedLayersForFind = false;

    includeLockedStoriesForFind = false;

    includeMasterPages = false;

    app.findGrepPreferences.properties = findProperties;

    var mySearch = applySearchTo.findGrep();

    app.findGrepPreferences = NothingEnum.nothing;

    return mySearch;

};

function fnUppercase(applySearchTo) {

    // here is problematic expression

    var myTexts = fnFindGrep({findWhat: '[.!\\?] \\l'}, applySearchTo);

    var myUpperTexts = [];

    var myFinds = [];

    for (var i = 0; i < myTexts.length; i++) {

        myTexts = myTexts.contents;

        myUpperTexts =

            myTexts.slice(0, myTexts.length - 1) +

            myTexts.slice(myTexts.length - 1, myTexts.length).toUpperCase();

        myFinds.push({

            strFindWhat: {findWhat: myTexts},

            strChangeTo: {changeTo: myUpperTexts}

        });

   

    }

    for (var j = 0; j < myTexts.length; j++) {

        fnFindChange(myFinds.strFindWhat, myFinds.strChangeTo, applySearchTo);

    }

}

fnUppercase(app.activeDocument);

When I'm using '[!\\?]\\l' not as argument of my function fnFindGrep, just as string of findWhat grep properietie, it works. But in my code it fails.

I'm trying to achive script that will take every lowercase character that fallows '?', '!', '.' and change it to uppercase.

jf

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jan 31, 2017 Jan 31, 2017

Copy link to clipboard

Copied

Have you noticed that you have an extra space in your code:

'[.!\\?] \\l'

You look for either a dot, a question or exclamation mark followed by a space then a lowercase character.

if this space is possibly here but not in all cases, use

'[.!\\?] ?\\l'

You may also try

'[!\\?] ?|\\K.'

'(?<=[!\\?] ).'

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Jan 31, 2017 Jan 31, 2017

Copy link to clipboard

Copied

Guesswork ahead!!!

If you run the code from the script panel it should work.

The issue you are facing is that ESTK passes the script to InDesign as a CDATA block in an xml, and it misshandles the '[!'

Alternatively, you could split the expression as:

var myTexts = fnFindGrep({findWhat: '['+'!?] \\l'}, applySearchTo); 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 15, 2017 Feb 15, 2017

Copy link to clipboard

Copied

Hi guys!

Sorry for the delay, I was traveling a bit.

I've tried all of your methods, non is working for me.

Zrzut ekranu 2017-02-15 o 09.21.44.png

Above is my exemplar text.

Loic.Aigon, thanks for spotting extra space in my code (in '[.!\\?] \\l') - but as you can see, it is no accident.

Vamitul, good to know that 'ESTK passes the script to InDesign as a CDATA block in an xml', I will bare this in mind next time, but I'm running discussed script straight from Id panel. Your approach with:

var myTexts = fnFindGrep({findWhat: '['+'!?.] \\l'}, applySearchTo);

works only for dot and exclamation mark, quotation mark is still missed.

Any other suggestions where I'm making mistake? Thanks!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Feb 15, 2017 Feb 15, 2017

Copy link to clipboard

Copied

Stepping through your code it's fairly easy to understand the mistake.

Let's use the following input text as example:

ala bala? portocala

ala bala. portocala

ala bala! portocala

Your initial grep search ('[.!?] \\l') works just fine, and it returns an 3 element array:
? p. p

! p

Then those three finds are used to create a new grep search, and I hope you can now already see the issue:

instead of searching for '\\? p' you are searching for just '? p' where the '?' is considered a regex special character (even more, the '. p' will replace any character followed by a space and a 'p' - nasty).

So, what to do:
Instead of using a changeGrep to convert the stuff to upercase simply iterate through the found results and fix the stuff:

function fnUppercase(applySearchTo) { 

    var myTexts = fnFindGrep({findWhat: '[.!?] \\l'}, applySearchTo),

    lastLetter; 

    for (var i = 0; i < myTexts.length; i++) { 

        lastLetter = myTexts.characters[-1]; 

        lastLetter.contents=lastLetter.contents.toUpperCase();

      }

  }

Easy-peasy!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 15, 2017 Feb 15, 2017

Copy link to clipboard

Copied

LATEST

Easy-peasy! - for you. Thanks a lot, it is exactly what I was looking for: much cleaner and straight-forward solution.

Because I'm not a professional, not even not a programmer, sometimes I tend to complicate things too much. Good to have all of you, guys!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 15, 2017 Feb 15, 2017

Copy link to clipboard

Copied

Try any of these:

[?!]\l

[!\x{003F}]\l

Since '?' question mark is a reserved character, the Find/Change might not be accepting it. Instead use its unicode value in your code.

Hope that resolves your problem.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 15, 2017 Feb 15, 2017

Copy link to clipboard

Copied

Thank you, Masood.Ahmad, it is very helpful remark.

In the end I like Vamitul approach better than mine.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines