Link to home
Start Free TrialLog in
Avatar of SSupreme
SSupremeFlag for Belarus

asked on

How to pull out the text written between symbols for each line from textarea?

How to pull out the text written between (any) symbols for each line from textarea using javascript.
I was thinking to use split() but didn't use it before enough. It needs to be specific enought to create array.

Example of textarea:
Mike Smith June 22 (happy)
Liza Gray May 01 (sad)
Peter Dec (lucky)

Result:
Array (happy,sad,lucky)

It's easier for me to do it excel but I need it to be done with javascript.
Hope you have a simple solution, Thanks.
SOLUTION
Avatar of Steve Bink
Steve Bink
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
console log it, or push to a new array. 'group' is the found element

var txt = "Mike Smith June 22 (happy) Liza Gray May 01 (sad) Peter Dec (lucky)";
txt.replace(/\(([^)]+)\)/g, function(m, group) { 
    console.log(group)
})

Open in new window


outputs:
 happy
 sad
 lucky
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Rainer,

Simply setting the global flag you will end up with the parenthesis included in the results. see jsfiddle: http://jsfiddle.net/em1mq9b5/

You need the call back function to extract the group (ie, no parenthesis). See above solution:

https://www.experts-exchange.com/questions/28590197/How-to-pull-out-the-text-written-between-symbols-for-each-line-from-textarea.html?anchorAnswerId=40530365#a40530365

or any other solution that loops over the resulting arrays:
 more examples:
http://jsfiddle.net/em1mq9b5/2/