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

RegexText EditorNotepad++

Regex Problem Overview


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

Regex Solutions


Solution 1 - Regex

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:

  1. Find (in regular expression mode):

     (.+)
    

    Replace with:

     "\1"
    

    This adds the quotes:

     "AliceBlue"
     "AntiqueWhite"
     "Aqua"
     "Aquamarine"
     "Azure"
     "Beige"
     "Bisque"
     "Black"
     "BlanchedAlmond"
    
  2. 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"
    

  3. Add the var myArray = assignment and braces manually:

     var myArray = ["AliceBlue", "AntiqueWhite", "Aqua", "Aquamarine", "Azure", "Beige", "Bisque", "Black", "BlanchedAlmond"];
    

Solution 2 - Regex

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"
    

Solution 3 - Regex

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

Solution 4 - Regex

  • 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.

Solution 5 - Regex

In notepad++, for placing any thing before value

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

In notepad++, for placing any thing After value

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

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

Happy Learning!!

Thanks.

Solution 6 - Regex

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!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMike SavView Question on Stackoverflow
Solution 1 - RegexBoltClockView Answer on Stackoverflow
Solution 2 - Regexuser3322017View Answer on Stackoverflow
Solution 3 - RegexabhisekpaulView Answer on Stackoverflow
Solution 4 - RegexGahlen FridleyView Answer on Stackoverflow
Solution 5 - RegexAshish GuptaView Answer on Stackoverflow
Solution 6 - RegexFrank LiView Answer on Stackoverflow