1

I need to remove the first character of a string "|" on each line in javascript.

| D| Gbm| G| A|
| D| Gbm| G| A|
| Bm| Gbm| G| D|
| Bm| Gbm| G| A|
| D| Gbm| G| A|
| D| Gbm| G| A|
| Bm| Gbm| G| D|
| Bm| E| G| G A|
| D| Gbm|

end results needed:

 D| Gbm| G| A|
 D| Gbm| G| A|
 Bm| Gbm| G| D|
 Bm| Gbm| G| A|
 D| Gbm| G| A|
 D| Gbm| G| A|
 Bm| Gbm| G| D|
 Bm| E| G| G A|
 D| Gbm|

This is the page I need to edit to get just the chords. I removed the quotes with

tOut = tOut.replace("\"", "");

http://www.lesession.co.uk/abc/abcChordsOut.htm This is the text input in ABC Notation format:

X:1
T:Guitar
L:1/8
K:D  
Q:1/4=120
R:Medium Boogie 
M:4/4
| "D"D,2 A,,D, D,2 [C,A,,]_B,,/2=B,,/2 | "Gbm"_G,,3 _G,,/2E,,/2 _G,,2 _D,/2=D,/2_E,/2=E,/2 | "G"G,3 G, G,3 =G,, | "A"A,,3 A,, A,,2 B,,2 |
| "D"A,,3 =D,/2A,,/2 D,2 F,G, | "Gbm"_G,,3 _G,,/2E,,/2 _G,,2 _D,_G,, | "G"=G,,3 G,,/2E,,/2 G,,2 B,,=D, | "A"A,2 G,A, A,E, A,,2 |
| "Bm"B,,2 =C,B,,/2A,,/2 B,,2 A,,2 | "Gbm"_G,3 _G,/2=F,/2 _G,2 _D,2 | "G"=G,3 =D, G,2 A,2 | "D"[D3^G,3] D D2 B,/2C/2A,/2^C,/2=C,/2 |
| "Bm"B,,3 B,,/2A,,/2 B,,3 F,,/2E,,/2 | "Gbm"_G,,3 _G,, E,,/2_G,,3/2 A,,_D, | "G"=G,,3 G,,/2E,,/2 G,,2 B,,/2G,,/2_B,,/2^G,,/2 | "A"A,,3 A,, A,,A,,=B,,C, |
| "D"D,3 D,/2A,,/2 D,2 A,2 | "Gbm"_G,,3 _G,,/2=F,,/2 _G,,3 E,, | "G"=G,,3 G,,/2E,,/2 G,,2 B,,3/2=C,/2 | "A"A,,4  z4 |
| "D" z3/2 B,,/2 ^C,D, A,,/2D,3/2- D,3/2A,/2 | "Gbm"_G,,3 _G,, E,,/2_G,,3/2 A,,_D, | "G"=G,,3 G,,/2E,,/2 G,,2 _B,,=D, | "A"A,,3 A,, A,,2 E,,2 |
| "Bm"B,2 B,2 [B,3D,3] F, | "Gbm"_G,,3 _G,,/2E,,/2 _G,,2 _D,_G,, | "G"=G,,3 G,,/2E,,/2 G,,2 =B,,[=D,A,,-- ] | "D"[D,3A,,3] D,  z4 |
| "Bm" z4  z2  z F,/2=F,/2 | "E"E,,3 E,, E,,2 B,,E,,/2G,,/2- | "G"G,,2- G,,/2G,,E,,/2 G,,3/2B,,/2- B,,2 | "G"_A,/2G,3/2 B,G, "A"=A,2 A,,2 |
| "D" z8 | "Gbm" z8 |

after it has been formatted it is pasted into the Java app Impro-Visor to give the chords.

0

3 Answers 3

4

Use a global (g), multi-line (m) RegExp to match any pipe (\|) at the start of a line (^)

// here's your original string
let tOut = `| D| Gbm| G| A|
| D| Gbm| G| A|
| Bm| Gbm| G| D|
| Bm| Gbm| G| A|
| D| Gbm| G| A|
| D| Gbm| G| A|
| Bm| Gbm| G| D|
| Bm| E| G| G A|
| D| Gbm|`

// replace the pipes
tOut = tOut.replace(/^\| ?/mg, '')

console.log(tOut)
// document.write(tOut)

The above also removes the space following the pipe if it's present. If you want to keep the space, change the regex to /^\|/mg.

Sign up to request clarification or add additional context in comments.

4 Comments

Yeah, that's a better idea.
Where do I put it in view-source:http://www.lesession.co.uk/abc/abcChordsOut.htm for document.write(tOut)
@LuaStart I've updated my answer to try and make it a little clearer
I saved the page from that website and running it locally, I inserted the 2 lines above // document.write(tOut) but I don't get the results. If you paste that ABC code into the offline page you will see what it does.
1

You can split the multiline string on the line break (\n), use map to remove the first character from each line, and join on the line break to convert the array back to a string. This works no matter what character each line starts with.

const str = `| D| Gbm| G| A|
| D| Gbm| G| A|
| Bm| Gbm| G| D|
| Bm| Gbm| G| A|
| D| Gbm| G| A|
| D| Gbm| G| A|
| Bm| Gbm| G| D|
| Bm| E| G| G A|
| D| Gbm|`;
const res = str.split("\n").map(s=>s.slice(1)).join("\n");
console.log(res);

Comments

0

This is the changed code that worked, prevent line break, remove the ":::|" and replaced "||" with "|"br""

tOut += "" //"<br>"

tOut = tOut.split(":::|").join("");
tOut = tOut.split("||").join("|<br>");
document.write(tOut)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.