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

6

131

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

Irremissible answered 13/1, 2012 at 10:48 Comment(0)
D
341

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"];
    
Dipetalous answered 13/1, 2012 at 11:3 Comment(8)
If you want to automate this, you can start recording a macro, then perform these steps.Dipetalous
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.Uitlander
On Windows, 2. Find (in extended mode): \r\nClearstory
@Rami A.: True, the source file might have been created in Windows so it would make sense to use Windows line endings. Note that 1) Notepad++ is Windows-only anyway, and even then, 2) that doesn't mean any file it opens will have Windows line endings.Dipetalous
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 <LI> menu and it was PERFECT! I would like to know some more possible tricks I could use.Misteach
This worked, but I had to search [\r\n] instead of \r\nNorvin
Since I just needed quotes at the start of each line I had to replace with this: "\1 (no trailing quote).Watkin
Strange. This did not work for me in VisualStudioCode. It replaced the entire emails with "\1" as a string replacement.Esperanzaespial
J
14
  • 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"
    
Jugate answered 18/2, 2014 at 6:24 Comment(1)
I tried these steps but ended up with extra "," after the last line in text.Vespid
A
13

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.

Ardeth answered 26/5, 2021 at 16:43 Comment(2)
By far the easiest to implement and simplest to understand. I spent way too long on trying to get various regex' to work and all I needed was to split it into beginning of line and end of line and not bother with what was in between them. You deserve a cookie!Pretypify
Sweet and simple. I started by placing the cursor at the beginning of the first line and it worked like a charm.Plonk
E
9
  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.
Endstopped answered 21/4, 2015 at 10:47 Comment(1)
I have spaces in my line text like 'Action and Adventure' or 'musical & classical'. Now the step 1 worked but step 3 resulted in breaking the string and inserted double quote inside the string like this."Actio"n & Adventure My original list look like this Anime Children & Family Action & Adventure British Classic & Cult Classic Comedies Crime Cult Documentaries Dramas Horror Independent International LGBTQ Music & Classical Reality Romantic Sci-Fi Sports Thrillers I am using 64 bits Notepad++ v8.4.4Vespid
A
6

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!

Anticipation answered 7/12, 2021 at 3:17 Comment(0)
S
5
  • 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.

Surfacetoair answered 15/6, 2012 at 16:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.