Community

    • Login
    • Search
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    Regex replacement of text within regex find

    Help wanted · · · – – – · · ·
    3
    3
    72
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Nick Minchin
      Nick Minchin last edited by

      I want to find text using regex and then replace some of the characters within the found strings…
      e.g. I have this text:

         "dataLabels": {
              "show": true,
              "name": {
                  "show": true,
                  "fontSize": "16px",
                  "fontFamily": "undefined",
                  "fontWeight": 600,
                  "color": "undefined",
                  "offsetY": -10
              },
              "value": {
                  "show": true,
                  "fontSize": "14px",
                  "fontFamily": "undefined",
                  "fontWeight": 400,
                  "color": "undefined",
                  "offsetY": 16,
                  "formatter": "function (val) { return val + " % " }"
              },
              "total": {
                  "show": false,
                  "label": "Total",
      

      and this regex expression:

      "formatter": function([\s\S]+?)}
      

      This will find this:

      "formatter": "function (val) { return val + " % " }"
      

      I want to replace the " % " with ' % '
      There could be any number of these formatter finds I need to replace.

      Not sure if it’s possible to do with npp?

      Alan Kilborn 1 Reply Last reply Reply Quote 0
      • Alan Kilborn
        Alan Kilborn @Nick Minchin last edited by

        @Nick-Minchin

        Reading between the lines, I would infer that you want to replace " % " but only when it occurs on the same line as "formatter": "function between exactly that and }" + line-ending. Is that correct?

        1 Reply Last reply Reply Quote 1
        • guy038
          guy038 last edited by guy038

          Hello @nick-minchin, @alan-kilborn and All,

          Here is the road map for this S/R :

          • Open your file in Notepad++

          • Go to the very beginning of this file ( Ctrl + Home )

          • Open the Replace dialog ( Ctrl + H )

          • SEARCH (?x-si) ( ^ \h* "formatter":\x20+"function | (?!\A) \G ) .*? \K " (?!$)

          • REPLACE '

          • Un-tick all options

          • Select the Regular expression search mode

          • Click on the Replace All button ( Do not use the Replace button !)

          Voila !

          This S/R will replace any non-trailing double quote ", only after the string "formatter": "function, with a simple quote ', in all the lines of your file

          Best Regards

          guy038

          1 Reply Last reply Reply Quote 0
          • First post
            Last post
          Copyright © 2014 NodeBB Forums | Contributors