ChatGPT解决这个技术问题 Extra ChatGPT

Add quotation at the start and end of each line in Notepad++

I have a list (in a .txt file) which I'd like to quickly convert to JavaScript Syntax, so I want to take the following:

AliceBlue
AntiqueWhite
Aqua
Aquamarine
Azure
Beige
Bisque
Black
BlanchedAlmond

and convert it to an array literal...

var myArray = ["AliceBlue", "AntiqueWhite", ... ]

I have the list in notepad++ and I need a reg expression to add the " at the start of the line and ", at the end and remove the line break... does anyone have a quick fix to do this? I'm terrible with RegEx.

I often have to perform such tasks so to know how to do this would be a great benefit to me. Many thanks


B
BoltClock

You won't be able to do it in a single replacement; you'll have to perform a few steps. Here's how I'd do it:

Find (in regular expression mode): (.+) Replace with: "\1" This adds the quotes: "AliceBlue" "AntiqueWhite" "Aqua" "Aquamarine" "Azure" "Beige" "Bisque" "Black" "BlanchedAlmond" Find (in extended mode): \r\n Replace with (with a space after the comma, not shown): , This converts the lines into a comma-separated list: "AliceBlue", "AntiqueWhite", "Aqua", "Aquamarine", "Azure", "Beige", "Bisque", "Black", "BlanchedAlmond" Add the var myArray = assignment and braces manually: var myArray = ["AliceBlue", "AntiqueWhite", "Aqua", "Aquamarine", "Azure", "Beige", "Bisque", "Black", "BlanchedAlmond"];


If you want to automate this, you can start recording a macro, then perform these steps.
Just before the "Find (in extended mode)step," verify/make the file end of line character (EOL) to UNIX format. Do Edit->EOL Conversion->UNIX format.
On Windows, 2. Find (in extended mode): \r\n
Is the \1 option Notepad++ specific or is it standard for RegEx? Where do you find a list of these? I used this for a HTML
  • menu and it was PERFECT! I would like to know some more possible tricks I could use.
  • This worked, but I had to search [\r\n] instead of \r\n
    u
    user3322017

    One simple way is replace \n(newline) with ","(double-quote comma double-quote) after this append double-quote in the start and end of file.

    example:

          AliceBlue
          AntiqueWhite
          Aqua
          Aquamarine
          Beige
    

    Replcae \n with "," AliceBlue","AntiqueWhite","Aqua","Aquamarine","Beige

    Now append "(double-quote) at the start and end "AliceBlue","AntiqueWhite","Aqua","Aquamarine","Beige"

    If your text contains blank lines in between you can use regular expression \n+ instead of \n

    example:

          AliceBlue
    
          AntiqueWhite
          Aqua
    
    
          Aquamarine
          Beige
    

    Replcae \n+ with "," (in regex mode) AliceBlue","AntiqueWhite","Aqua","Aquamarine","Beige

    Now append "(double-quote) at the start and end "AliceBlue","AntiqueWhite","Aqua","Aquamarine","Beige"


    Simple and effective.
    Thanks, I tried the same and ended up searching for better way, ended here. This is plain and simple
    a
    abhisekpaul

    Put your cursor at the begining of line 1. click Edit>ColumnEditor. Put " in the text and hit enter. Repeat 2 but put the cursor at the end of line1 and put ", and hit enter.


    This is the easiest way to do it!
    S
    Stephan

    Place your cursor at the end of the text.

    Press SHIFT and ->. The cursor will move to the next line.

    Press CTRL-F and type , in "Replace with:" and press ENTER.

    You will need to put a quote at the beginning of your first text and the end of your last.


    Very Useful and short. Thanks!
    D
    Dharman

    In notepad++, for placing any thing before value

    Press CTRL+H Replace ^ with ' (sign you want append at the start) Select search mode as Regular Expression Click Replace All

    In notepad++, for placing any thing After value

    Press CTRL+H Replace $ with ' (sign you want to append at the end) Select search mode as Regular Expression Click Replace All

    Ex: After performing above steps AHV01 replaced with 'AHV01'

    Happy Learning!!

    Thanks.


    F
    Frank Li

    I am using Notepad 8.1.9.2 64bit on Windows10, the replacement procedures can be finished in one step, try this:

    Find what: (.+)\r\n

    Replace with: "\1",

    Note: Wrap around and regular express option is selected.

    And then you still need to add bracket manually in your code

    Thanks!