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
If we want to insert double quotes into a file we can do this by specifying Chr(34); the Chr function takes the ASCII value – 34 – and converts it to an actual character (in this case double quotes).
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With