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
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:
This adds the quotes:
Find (in extended mode):
Replace with (with a space after the comma, not shown):
This converts the lines into a comma-separated list:
Add the
var myArray =
assignment and braces manually:example:
Replcae \n with ","
Now append "(double-quote) at the start and end
If your text contains blank lines in between you can use regular expression \n+ instead of \n
example:
Replcae \n+ with "," (in regex mode)
Now append "(double-quote) at the start and end
You will need to put a quote at the beginning of your first text and the end of your last.