edward edward - 21 days ago 6
Javascript Question

I'm trying to find and match a word/string and get the value after the comma

I have a cascading list (dynamic - could be 10 or 100) and I need to find and match a specific word/string and grab the value after the comma(see below)




eddy,Triangle


joe,Drum


sarah,Keyboard


jill,Guitar


bob,Bass




As an example, I want to find and match the string sarah and grab the word Keyboard, any ideas on how I can do this?

Answer

To get the string after sarah,:

str = "eddy,Triangle\n\
joe,Drum\n\
sarah,Keyboard\n\
jill,Guitar\n\
bob,Bass";

re = new RegExp('sarah,(.*)')
console.log(str.match(re)[1]);