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: "\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"];
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"
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.
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.
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.
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!
Success story sharing
[\r\n]
instead of\r\n