replaceText() RegEx “not followed by”

后端 未结 3 1899
无人共我
无人共我 2020-12-21 00:07

Any ideas why this simple RegEx doesn\'t seem to be supported in a Google Docs script?

foo(?!bar)

I\'m assuming that Google App

3条回答
  •  清酒与你
    2020-12-21 00:36

    You have a sequence which you can match with a regular expression, but that regular expression will also match one, or more, things which you do not desire to change. The generalized solution to this situation is to:

    1. Change the text such that you have known sequences of characters which are definitely not used. Effectively, this gives you sequences of characters which you use as variables to hold the values you don't want to change. Personally, I would use:
      body.replaceText('Q','Qz');
      Which will make it such that there is no sequence in your document which matches /Q[^z]/. This results in you being able to use sequences like Qa to represent some text you don't want to change. I use Q because it has a low frequency of use in English. You can use any character. For efficiency, choose a character which results in a low number of changes within the text you are affecting.
    2. Change the things you don't want to end up changing to one of the character sequences you now know are unused. For example:
      body.replaceText('foobar','Qa');
      Repeat this for whatever additional items you don't want to end up changing.
    3. Change the text you are really wanting to change. In this example: body.replaceText('foo','hello'.replace(/Q/g,'Qz'));
      Note that you need to apply to the new replacement text the first substitution which you used to open up known unused sequences.
    4. Restore all of the things you did not want to change to their original state:
      body.replaceText('Qa','foobar');
    5. Restore the text you used to open up unused character sequences:
      body.replaceText('Qz','Q');

    All together that would be:

    var body = DocumentApp.getActiveDocument().getBody();
    body.replaceText('Q','Qz');      //Open up unused character sequences
    body.replaceText('foobar','Qa'); //Save the things you don't want to change.
    
    //In the general case, you need to apply to the new text the same substitution
    //  which you used to open up unused character sequences.  If you don't you
    //  may end up with those sequences being changed in the new text.
    body.replaceText('foo','hello'.replace(/Q/g,'Qz')); //Make the change you desire.
    
    body.replaceText('Qa','foobar'); //Restore the things you saved.
    body.replaceText('Qz','Q');      //Restore the original sequence.
    

    While solving the problem this way does not allow you to use all the features of JavaScript RegExp (e.g. capture groups, look-ahead assertions, and flags), it should preserve the formatting within your document.

    You can choose not to perform steps 1 and 5 above by picking a longer sequence of characters to use to represent the text which you do not want to match (e.g. kNoWn1UnUsEd). However, such a longer sequence is something that must be selected based on your knowledge of what already exists in the document. Doing that can save a couple of steps, but you either have to search for an unused string or accept that there is some probability that the string you use is already in the document, which would result in an undesired substitution.

提交回复
热议问题